Where do you do your validation? model, controller or view

后端 未结 10 892
生来不讨喜
生来不讨喜 2020-12-25 07:52

Where do you put user input validation in a web form application?

  1. View: JavaScript client side
  2. Controller: Server side language (C#...)
  3. Model
10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 08:45

    Validation in the model, optionally automated routines in the UI that take their hints from the model and improve the user experience.

    By automated routines I mean that there shouldn't be any per-model validation code in the user interface. If you have a library of validation methods, such as RoR's (which has methods like validates_presence_of :username) the controller or view should be able to read these and apply equivalent javascript (or whatever is convenient) methods.

    That means you will have to duplicate the complete validation library in the ui, or at least provide a mapping if you use a preexisting one. But once that's done you won't have to write any validation logic outside the model.

提交回复
热议问题