jpkleemans / angular-validate

Painless form validation for AngularJS. Powered by the jQuery Validation Plugin.
MIT License
68 stars 33 forks source link

Dynamic validator #20

Closed deniz946 closed 7 years ago

deniz946 commented 7 years ago

Hello, I was trying to make the validation a bit dynamic but it seem that it's not working or I'm not able to make it work, let me explain myself.

I have a form with 6 inputs and 1 check, the first 3 inputs are required always and the other 3 are required only if you check the checkbox, so I've did something like this

$scope.validator = { rules: { input1:{required: true}, input2:{required: true}, input3:{required: true}, input4:{required: false}, input5:{required: false}, input6:{required: false}, }

$scope.$watch('checkbox', function(check){ if(check){ $scope.validator.rules.input4.required = true $scope.validator.rules.input5.required = true $scope.validator.rules.input6.required = true } )

And finally $scope.save = functino(myForm){ return myForm.validate() }

And save returns true if have just the first 3 inputs filled ( it doesnt check for the other last 3 inputs)

Any possible solution for this? Thanks

jpkleemans commented 7 years ago

Hi,

You can only pass the validation options once; the plugin doesn't update them dynamically.

Maybe this can help with your problem: http://jqueryvalidation.org/category/methods/#example:-makes-details-required-only-if-#other-is-checked