Data Validation in MVC

Suppose I have a “View” to fill out a DVD rental form, according to the MVC architecture, either “Controller” or “Model”, which should check the form data? Thanks

+4
source share
3 answers

You should be in the Model MVC section. Since models have different fields, only models can know which combination of inputs makes this model valid. It’s not just about whether an empty field or input of this field corresponds to some template, but sometimes it’s a combination of field inputs or the relation of the model to other models that determine the correct state.

+2
source

All 3 are usually involved in the verification process if you follow a typical flow.

The model defines validation attributes, such as required attributes or stringlength attributes. The controller checks the validation status of the model using ModelState.IsValid and makes appropriate decisions. A view may optionally provide client-side validation for the same attributes. Do not rely solely on js to validate your form.

+1
source

My suggestion was to validate in a view with some form of validation binding, and then again in the model before it remains in any data store.

0
source

Source: https://habr.com/ru/post/1394547/


All Articles