Angular2 dynamically update validation rules - validation

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.

Related

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

skip all ModelForm validation conditionally but include it all otherwise

I have a form that has a selector at the top where a user can select none, or choose a couple options where the validation varies depending on the selection.
If they select none, then it needs to save the model ignoring all the fields and just putting in "None" for the one option. If they select one of the others, then it needs to preform validation on all the other fields.
The thing is, the only way for me to get it to save when "None" is selected, is if I set blank=True on all the fields for the model. So I basically have to turn off all the validation.
But then when they select something, I override the clear method and have to do all the validation manually...
Is there any way for me to use all the built in validation based on the option? In other frameworks I've used, there's an array full of rules, and I can just modify that based on whatever and then run the rules automatically off that array.
Is there a method before the clean, clean_fieldname, and any other validation where I can grab the one selected option, and modify the rules for everything else based on it before it does the build in validation?
I just can't seem to figure out how to use the validation in a versatile way in Django. It seems that if I'm not using them in one specific scenario, it falls down to having to do it all manually. I am fairly new to it, so I'm likely missing something.
Thanks

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

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.

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

Validate data from CakePHP form with jQuery (AJAX)

I would like to validate both single field and multiple field data from a CakePHP form.
The single field validation should be done on blur from each field while the multiple field validation should be done on submitting the form.
I would like to use the $validate property declared in the Model for validating data and I would like to display the errors near each field (single field validation) and on top of the form (for multiple field validation).
My main goal is to achieve this the most "caky" way (if there is one for validating data with jQuery). I couldn't find any useful advice out there and I'm asking you for some help to get this going.
One of my concerns is how shall I pass data from the form to jQuery and then to the action that does the validation and also how shall I return and display the errors, if there are any.
Thank you in advance!
I'd suggest first making sure everything works without jQuery, then use the jQuery Form plugin to submit your forms via AJAX. If you include the RequestHandler component in your AppController, you should find that your controllers distinguish automatically between AJAX and synchronous requests.
OK, so I coded my own solution to this, but I am still waiting for a more "caky" approach.
I made two generic jQuery functions, one for single field validation and one for multiple field validation. The function should grab the data from the specified form and send it to the form's action via AJAX, to a specially created controller method which will attempt to validate data and output an AJAX response ("" for validation has passed and errors for errors in validation). Then, the result is checked in the jQuery function and the default form behaviour is triggered only if the validation has passed. Otherwise, display the errors and return false; to prevent default submission.

Resources