Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation strategies with AngularJS

I'm evaluating AngularJS and so far I'm very enthusiastic about it. But there's something missing on the validation front: the available options, such as the built-in mechanisms and the AngularUI initiative, implement validators through directives and, as such, every validation should be declared in the view:

<form ng-controller="SomeController">
    <!-- Notice the 'required' attribute directive below: -->
    <input type="text" ng-model="user.name" name="uName" required />
</form>

In this example, the view is defining that user.name is required. It's like saying the view defines the proper shape of the model. Isn't it a little backwards? Shouldn't the view reflect the states, including error states when it's the case?

Am I mistaken? I'm wondering if it's possible to apply any validators in the controller, signaling the model's data as valid/invalid, and updating the view accordingly (painting form controls with red, showing error messages, clearing previous errors and so on). I'm assuming AngularJS is powerful enough for this, but in the docs and samples so far I just haven't seen anything like I've described above. Thanks!

like image 785
Humberto Avatar asked Oct 05 '12 18:10

Humberto


People also ask

How is validation done in AngularJS?

AngularJS performs form validation on the client side. AngularJS monitors the state of the form and input fields (input, text-area, select), and notify the user about the current state. AngularJS also holds information about whether the input fields have been touched, modified, or not.

What are validation in angular?

Angular uses directives to match these attributes with validator functions in the framework. Every time the value of a form control changes, Angular runs validation and generates either a list of validation errors that results in an INVALID status, or null, which results in a VALID status.

Which of the following are validation directives?

AngularJS includes the following validation directives. Sets required attribute on an input field. Sets minlength attribute on an input field. Sets maxlength attribute on an input field.

What is form validation?

Form validation is a “technical process where a web-form checks if the information provided by a user is correct.” The form will either alert the user that they messed up and need to fix something to proceed, or the form will be validated and the user will be able to continue with their registration process.


1 Answers

I guess its all about perspective. The way I see it is, you are defining a view which contains a form and that form contains an input of type text. It is this text input that you are marking as required. If you note, angular does not care if the text is user.name or user.age or whatever else. Its just associating that text input with required. So its just validating that text input and the model associated with that model is the final result ( the place where the value goes in if the validation passes! ).

Have a look at

http://docs.angularjs.org/guide/forms

for custom form validations, if you want to to be doing validations that are not the default ones.

Since you already know the view that is getting produced in advance ( lets call it at compile time! ) , you can associate all validators in the view and hence wouldnt have to do it in the controller (which perhaps is for run-time validation! ).

like image 129
ganaraj Avatar answered Sep 23 '22 15:09

ganaraj