Validating model

If you want to change the validation logic later, you can do so in exactly one place by adding validation attributes to the model (in this example, the class).You won't have to worry about different parts of the application being inconsistent with how the rules are enforced — all validation logic will be defined in one place and used everywhere.Here's a complete code listing for the updated Notice how the form has automatically used a red border color to highlight the text boxes that contain invalid data and has emitted an appropriate validation error message next to each one.The errors are enforced both client-side (using Java Script and j Query) and server-side (in case a user has Java Script disabled).NET Framework helps make your application more robust.It also ensures that you can't forget to validate something and inadvertently let bad data into the database.The following image shows how to disable Java Script with the Chrome browser. They automatically look for validation attributes specified on the model and display error messages as appropriate.

Simply hitting the submit button without entering any of the fields will trigger client side validation.

A real benefit is that you didn't need to change a single line of code in the view in order to enable this validation UI.

The controller and views you created earlier in this tutorial automatically picked up the validation rules that you specified by using validation attributes on the properties of the , the required attribute is not enforced until you submit the form (hit the Create button), or enter text into the input field and removed it.

Deselecting these options will ignore these conditions.

model, and you'll ensure that the validation rules are enforced any time a user attempts to create or edit a movie using the application. You can declaratively specify validation rules in one place (in the model class) and the rules are enforced everywhere in the application.

Leave a Reply