MVC - 在哪里实现表单验证(服务器端)?

时间:2022-12-09 13:41:17

In coding a traditional MVC application, what is the best practice for coding server-side form validations? Does the code belong in the controller, or the model layer? And why?

在编码传统的MVC应用程序时,编码服务器端表单验证的最佳实践是什么?代码是属于控制器还是模型层?为什么?

4 个解决方案

#1


4  

From Wikipedia:

来自*:

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.

模型 - 视图 - 控制器(MVC)是软件工程中使用的架构模式。成功使用该模式将业务逻辑与用户界面考虑因素隔离开来,从而使应用程序更容易修改应用程序的可视外观或基础业务规则而不会影响另一个。在MVC中,模型表示应用程序的信息(数据)和用于操作数据的业务规则;视图对应于用户界面的元素,例如文本,复选框项等等;并且控制器管理涉及与用户动作模型的通信的细节,例如击键和鼠标移动。

Thus, model - it holds the application and the business rules.

因此,模型 - 它包含应用程序和业务规则。

#2


4  

I completely agree with Josh. However you may create a kind of validation layer between Controller and Model so that most of syntactical validations can be carried out on data before it reaches to model.

我完全赞同约什。但是,您可以在Controller和Model之间创建一种验证层,以便大多数语法验证可以在数据到达模型之前对数据执行。

For example,

例如,

The validation layer would validate the date format, amount format, mandatory fields, etc...

验证层将验证日期格式,金额格式,必填字段等...

So that model would purely concentrate on business validations like x amount should be greater than y amount.

因此,该模型将完全专注于业务验证,如x金额应大于y金额。

#3


0  

My experience with MVC thus far consists of entirely rails.

到目前为止,我对MVC的经验完全由rails组成。

Rails does it's validation 100% in the Model.
For the most part this works very well. I'd say 9 out of 10 times it's all you need.

Rails在模型中100%验证。在大多数情况下,这非常有效。我会说10次中的9次就是你所需要的。

There are some areas however where what you're submitting from a form doesn't match up with your model properly. There may be some additional filtering/rearranging or so on.

但是,在某些方面,您从表单提交的内容与您的模型不匹配。可能会有一些额外的过滤/重新排列等。

The best way to solve these situations I've found is to create faux-model objects, which basically act like Model objects but map 1-to-1 with the form data. These faux-model objects don't actually save anything, they're just a bucket for the data with validations attached.
An example of such a thing (in rails) is ActiveForm

我发现解决这些情况的最好方法是创建仿模型对象,它们基本上像模型对象一样,但是与表单数据一对一地映射。这些仿模型对象实际上并没有保存任何东西,它们只是附加验证的数据的存储桶。这种事物(在rails中)的一个例子是ActiveForm

Once the data gets into those (and is valid) it's usually a pretty simple step to transfer it directly across to your actual models.

一旦数据进入那些(并且是有效的),将它直接传输到实际模型通常是一个非常简单的步骤。

#4


0  

The basic syntax check should be in the control as it translates the user input for the model. The model needs to do the real data validation.

基本语法检查应该在控件中,因为它会转换模型的用户输入。该模型需要进行真正的数据验证。

#1


4  

From Wikipedia:

来自*:

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.

模型 - 视图 - 控制器(MVC)是软件工程中使用的架构模式。成功使用该模式将业务逻辑与用户界面考虑因素隔离开来,从而使应用程序更容易修改应用程序的可视外观或基础业务规则而不会影响另一个。在MVC中,模型表示应用程序的信息(数据)和用于操作数据的业务规则;视图对应于用户界面的元素,例如文本,复选框项等等;并且控制器管理涉及与用户动作模型的通信的细节,例如击键和鼠标移动。

Thus, model - it holds the application and the business rules.

因此,模型 - 它包含应用程序和业务规则。

#2


4  

I completely agree with Josh. However you may create a kind of validation layer between Controller and Model so that most of syntactical validations can be carried out on data before it reaches to model.

我完全赞同约什。但是,您可以在Controller和Model之间创建一种验证层,以便大多数语法验证可以在数据到达模型之前对数据执行。

For example,

例如,

The validation layer would validate the date format, amount format, mandatory fields, etc...

验证层将验证日期格式,金额格式,必填字段等...

So that model would purely concentrate on business validations like x amount should be greater than y amount.

因此,该模型将完全专注于业务验证,如x金额应大于y金额。

#3


0  

My experience with MVC thus far consists of entirely rails.

到目前为止,我对MVC的经验完全由rails组成。

Rails does it's validation 100% in the Model.
For the most part this works very well. I'd say 9 out of 10 times it's all you need.

Rails在模型中100%验证。在大多数情况下,这非常有效。我会说10次中的9次就是你所需要的。

There are some areas however where what you're submitting from a form doesn't match up with your model properly. There may be some additional filtering/rearranging or so on.

但是,在某些方面,您从表单提交的内容与您的模型不匹配。可能会有一些额外的过滤/重新排列等。

The best way to solve these situations I've found is to create faux-model objects, which basically act like Model objects but map 1-to-1 with the form data. These faux-model objects don't actually save anything, they're just a bucket for the data with validations attached.
An example of such a thing (in rails) is ActiveForm

我发现解决这些情况的最好方法是创建仿模型对象,它们基本上像模型对象一样,但是与表单数据一对一地映射。这些仿模型对象实际上并没有保存任何东西,它们只是附加验证的数据的存储桶。这种事物(在rails中)的一个例子是ActiveForm

Once the data gets into those (and is valid) it's usually a pretty simple step to transfer it directly across to your actual models.

一旦数据进入那些(并且是有效的),将它直接传输到实际模型通常是一个非常简单的步骤。

#4


0  

The basic syntax check should be in the control as it translates the user input for the model. The model needs to do the real data validation.

基本语法检查应该在控件中,因为它会转换模型的用户输入。该模型需要进行真正的数据验证。