Yii: How do you abort model validation in the middle, after one of the rules returns FALSE? - validation

In Yii framework, how do you abort any further validation after one of rules returns FALSE ?
What I am trying to achieve is:
1) stopping unnecessary MySQL queries after we know that a model didn't pass the validation.
2) cleaner, easy to understand error messages for the web user, without sorting them manually in the controller.
Thank you in advance for your help!

In short: there is no global setting or solution: If you look at CActiveRecord::validate(), you can see all validators are called and executed.
You can prevent running multiple validations for the same attribute. You would have to set skipOnError=true for all the validation rules.
http://www.yiiframework.com/doc/api/1.1/CValidator/#skipOnError-detail
whether this validation rule should be skipped when there is already a
validation error for the current attribute. Defaults to false.
I think a global option could be added to Yii (quite easily actually).

Thank you!
For users browsing this thread:
The validate() method - to be extended in your custom AR class - is located in yii/framework/base/CModel, line 150.

Related

Angular2 dynamically update validation rules

In Angular2 I need to modify validation rules dynamically.On the selection of a value from a dropdown validation rules will be fetched from the database and they would be like
{filedName : FirstName, required : 1}.
Currenlty I'm suing template driven form validation but after searching a bit I have realised that I would have to use model driven approach. Any ideas on how to do that?
Edit
Answer by JayChase to the above mentioned question seems the right one in my scenario. But I have couple of queries.I can remove all validation rules by passing null to setValidators but how can I just remove required field validation.And is it possible for me to modify validation rules in template driven approach.
Update
I'm able to update validation rules by following JayChase's answer.I did not use async validation as it would have costed me too much server calls. So now the issue is how to update just the required field validation rule.Right now I'm removing all validations from the control.
form.controls["fieldName"].setValidators(null)
Any help on that would be much appreciated.

Laravel validator vs requests

Hello,
I want to understand how to handle data validation with Laravel 5. I see that this can be done using or the validator, or the request files. The thing is that there are many points I didn't get.
What is the difference between using a request file for validation or the validator class ?
If I have validation conditions, and I want to use them only if the concerned field was submitted, how can I do that ? If I use the "required" keyword, it won't work because it will fail when the field is not submitted. If I don't use it, it will accept empty strings...
Thanks ahead !
1. Theoretically there is no difference between Controller validation and Validation using FormRequest. Normally you should use FormRequest. This will keep your controller clean and Minimal. But some time it is sensible to use Validator within controller, e.g you know there is going to be just one field to validate, then it would be overkill to use FormRequest. So it is a matter of preferance.
2. You don't have to use 'required' if the field is not required. Other validation for that field will still run if that field is submitted. If not submitted nothing will happen.
.......
'money' => 'numeric',
.......
Above Rule will make sure that money field is numeric only if it is submitted. If no submitted no validation error will be thrown.
I hope this helps.
Request classes are the better way to validate requests, because
they help to extract this functionality from the constructor method,
which should be as clean as possible.
Use 'sometimes' validator. http://laravel.com/docs/5.1/validation#conditionally-adding-rules

Symfony2 validation filters

In my Symfony 2 application I need to filter input before passing it on to validation [1], however, I can't seem to find any system within Symfony to do this.
The type of filtering I looking for is e.g. to be able to filter a dash out of a specific field before validating it. E.g. users can enter 123-123 but the only accepted value is 123123. Just as I can set up validation rules with constraints, I'm looking for something similar for filters.
[1] http://symfony.com/doc/current/book/validation.html
Nifr's answer is good but is missing of an important alternative that, if I understand correctly your question, seems to fit perfectly your needs.
You can use a hook that is pretty much an event listener: if something happens or is going to happen, it intercepts the event and redirect it to your function.
In this case, you need a PRE_BIND hook (is deprecated since 2.3 version, now it's called PRE_SUBMIT)
Read this if you need help about
Either write your own Validation Assert to filter and then proxy the other validators for this purpose ...
... or one or multiple Regex Asserts.
... or use a DataTransformer to transform/filter the input.
With the DataTransformer involved you could aswell consider creating a new FieldType which renders two inputs with a seperator like the date form-field does. ( if not not used with widget => single_text )

How to disable “the error the value ‘abc’ is not valid for IntegerProperty”

I hope someone can help me out to disable the default validation that MVC 3 runs when I post a string value in an integer field. Currently the application will add the error “the value ‘abc’ is not valid for IntergerProperty” to the ModelState before our validators are executed.
We don’t use client side validation and have our own validators that are loaded in the Global.asax. We only want to use these validators to check the input and would like to disable this check.
Is it possible to disable this behavior?
Thanks in advanced,
André
I think the best solution for your issue is to implement a custom model binder to override the default behavior if you really want/need to be able to take alpha chars in a numeric field.

Remote Validation with MVC3

I've just been reading the article on MSDN about remote validation. This is great, but it only shows validating a specific property value.
Is there a way I can pass other values from my model into the validation for a particular property? For example, let's say that a user wants to cancel a number of items off an order - they should be prevented from entering a figure greater than the original order amount.
Thanks
No, you can't.
Brad Wilson:
At this time, only property level
validators can emit client-side
validation (as that lines up much
better with the idea of input
validation in the form of the
browser... there is no "model" to
speak of, from the browser's point of
view).
Stuart Leeks:
I don't believe you can hook up client
validation with IValidatableObject
Well, i am nit sure if you mean this, but you can use AdditionalFields with your RemoteValidation attribute.
Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

Resources