how do I cancel an update for an entity in RESTier that fails custom validation logic? - restier

I am using RESTier 0.4.0-rc2.
With OnUpdating... if the entity fails my custom validation logic I have no way to cancel the update, and return a custom error.
With CanUpdate... I can cancel the update by returning false, but there is no entity passed in to apply my custom logic to, and no way to provide a custom error.
Seems like a fundamental flaw, am I missing something?

Even you already have the answer, I would like to provide it for others.
If you want some customized logic for update validation, you can
implement a class implements interface IChangeSetEntryValidator,
validate in any logic you want, and then add logic like
DataModificationEntry dataModificationEntry = entry as DataModificationEntry;
var entity = dataModificationEntry.Entity;
// Customized validate logic and if error, add a error validation result.
validationResults.Add(new ChangeSetValidationResult()
{
Id = dataModificationEntry.EntitySetName+ dataModificationEntry.EntityKey,
Message = "Customized error",
Severity = ChangeSetValidationSeverity.Error,
Target = entity
});
You may find complete discussion at here

Related

Hibernate Validator Depends on

Is there a way to achieve depends on behavior with hibernate validation. For instance if i have two custom validations
#InvalidAmount // Validates the amount is invalid with some custom logic
#AmountNotAccepted // Validates the currency is not accepted along with some custom logic
The idea is not to merge both of them together and throw second error only if the first one succeeds. Is there a way to do it? Something like run the second validate only first first is not an error.
For example:
#AmountNotAccepted(dependsOn = {InvalidAmount})
You may be after group sequences?

Forcing Symfony form validation without actual submission

Is it possible to perform form fields validation manually, forced?
I have a form. It has a global form validation. Everything works well if user submits data.
But I want to trigger validation before form is displayed to the user - show errors before submit.
Tried to submit using $form->submit([]) method but it doesn't trigger form fields validation.
Any ideas on this issue? Did I something wrong?
Do you need to validate a data only by form? Doesn't a validator service work for you?
Like
$violations = $this->get('validator')->validate($entity);
Reason was quite complicated and simple at once.
// form instantiation
$type = new MyType();
$options = [
'csrf_protection'=>!empty($_POST[$type->getName()])
];
$form = $this->createForm($type, [/* or entity */], $options);
$form->handleRequest($request);
if(!$form->isSubmitted()){
$form->submit([]);
}
And now I can see errors correctly. One of the most tricky part is the fact I wasn't aware Form $options are read-only after creation and empty check is mandatory if you want to leave CSRF protection turned on.
Invoke $form->isValid(); before showing the form.
However If data come from an external service, I would rather prevent (with ws validation, for example) users to submit wrong data through ws (ws = external service)

Symfony 2 : validate console command arguments

I am creating a command to generate accounts from a file. In command I have passed some arguments.
$this
->setName('batch:create')
->setDescription('xyz')
->setHelp('xyz')
->addArgument('account-id', InputArgument::REQUIRED, "Set the account id.")
->addArgument('name', InputArgument::REQUIRED, "Set the account name.");
I was just thinking if there is any way I can check type of argument passed. For now I am checking it like this,
if (is_numeric($input->getArgument('account-id'))) {
// ....
}
Is there anyway I can create a validator that checks the type and I just have to call validate function.
if ($input->validate() === false) {
// show error message and return.
}
Unfortunately, currently there's no way to implement command argument validation in Symfony. The best way to implement these checks would be overriding Symfony\Component\Console\Command::initialize method in your command and then applying the validation rules there, throwing exceptions if passed arguments are invalid.
Update: Matthias Noback has implemented symfony-console-form (https://github.com/matthiasnoback/symfony-console-form), and looks like implementing Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand interface would give you basic validation abilities through the form component (have to test it with validation, though).

ASP.NET Web Pages 2 Select Value Validation

I figure this must be so simple and I'm missing something really obvious. I want to validate the selected value of a select input on an ASP.NET Web Pages 2 form using the built in validators but it doesn't look possible so far.
For example:
Validation.Add("my-select", Validator.ValueEquals("Some Value"));
Where Validator.ValueEquals would compare the selected value to the supplied parameter value "Some Value". I realize I could do:
if(Request["my-select"] != "Some Value") {
Validation.AddFormError("Invalid option selected");
}
But then I don't have the error message associated with the field and it will only appear if I'm rendering the validation summary at the top of the form.
What am I missing?
I've cracked the code! Unfortunately the solution is outside of the scope of the built in Validation and Validators static methods but I was able to achieve what I needed by using the following in place of the using the Validation class.
if(Request["my-select"] != "Some Value") {
ModelState.AddError("my-select", "Invalid option selected");
}
Then check if the ModelState is valid when checking Validation.IsValid():
if(ModelState.IsValid && Validation.IsValid()) {
// More codes...
}
It's kind of cool that ModelState is available but it really seems clunky that this isn't handled by the Validation and Validator classes.
You could use the EqualsTo validator and compare the supplied value to a hidden field value, or if you are worried that users might tamper with the hidden field value, you can write your own custom validator. I've blogged how to do that: http://www.mikesdotnetting.com/Article/195/ASP.NET-Web-Pages-Creating-Custom-Validators

How to update the value of a single field invoking appropriate validation

I'm making a module to allow users to update single fields on in this case, their user entity.
The code below is an example of the method I have initially been using to get it working and test other elements of the module
global $user;
$account = user_load($user->uid);
$edit = (array) $account;
$edit['field_lastname']['und'][0]['value'] = 'test';
user_save($account, $edit);
However this bypasses any field validation defined elsewhere in Drupal. I don't want to reproduce any validation written elsewhere - it's not the Drupal way!
My question is: Is there a function in Drupal 7 that can be called to update the value of a single field. I imagine such a function would clear the appropriate caches, invoke the fields validation etc.
I am aware the solution will be totally different to my current user object based one. I just can't for the life of me find the appropriate function in the API. I wander whether the fact I am looking for a save function alone is the problem - and that there are some other necessary steps that come before.
Any help gratefully appreciated.
Check out the drupal_form_submit function. It lets you submit forms from code. In this case, you could use it to the user edit form, which would then fire the appropriate validation.

Resources