简体   繁体   English

HMVC中的用户输入验证

[英]User input validation in HMVC

After reading countless answers on this subject, i feel that i have still not found an unequivocal answer for the question - where and how should I do the user input validation in a (H)MVC? 在阅读了关于该主题的无数答案之后,我觉得我仍然没有找到明确的答案-在(H)MVC中我应该在哪里以及如何进行用户输入验证? Going by the popular opinion i am partially sold on the idea of writing the validation code in MODEL , but that left the following two questions unanswered for me. 根据流行观点,我对在MODEL中编写验证代码的想法有些不满,但这使我没有回答以下两个问题。

1) How does the MODEL know which UI element (eg:- textbox with id user_name) is responsible for error, so that it can make the VIEW to put the focus on that particular UI element or display an error message beside it ? 1)MODEL如何知道哪个UI元素(例如,ID为user_name的文本框)引起错误,以便它可以使VIEW将焦点放在该特定UI元素上或在其旁边显示错误消息?

2) I read somewhere in SO that once you put the validation routines in MODEL , you could leverage the JS to place direct calls to those MODEL methods. 2)我在SO的某处读到,一旦将验证例程放入MODEL中,就可以利用JS来直接调用那些MODEL方法。 In that case wouldn't it be like breaking the basic rules of MVC pattern? 在那种情况下,难道就不是打破MVC模式的基本规则吗?

If MODEL is not right place to accommodate the validation code, where would you suggest? 如果MODEL不适用于验证代码,那么您会提出什么建议?

Please share your thoughts. 请分享您的想法。

Thanks, 谢谢,

1) How does the MODEL know which UI element (eg:- textbox with id user_name) is responsible for error, so that it can make the VIEW to put the focus on that particular UI element or display an error message beside it ? 1)MODEL如何知道哪个UI元素(例如,ID为user_name的文本框)引起错误,以便它可以使VIEW将焦点放在该特定UI元素上或在其旁边显示错误消息?

The model doesn't know (and doesn't need to know) which UI element is responsible. 该模型不知道(也不需要知道)哪个UI元素负责。 The Kohana ORM validation functions are designed to return an array of errors back to the controller, which the controller inserts into the view. Kohana ORM 验证功能旨在将错误数组返回给控制器,控制器将其插入视图中。 The Kohana documentation has an example of exactly what you are trying to do. Kohana文档中有一个您正在尝试执行的示例

2) I read somewhere in SO that once you put the validation routines in MODEL , you could leverage the JS to place direct calls to those MODEL methods. 2)我在SO的某处读到,一旦将验证例程放入MODEL中,就可以利用JS来直接调用那些MODEL方法。 In that case wouldn't it be like breaking the basic rules of MVC pattern? 在那种情况下,难道就不是打破MVC模式的基本规则吗?

Kohana does not have any built-in JavaScript-based model validation. Kohana没有任何内置的基于JavaScript的模型验证。 There are many add-on JavaScript validation frameworks you can use, and most of the common ones work by having you generate a list of class names, a special data-* attribute, or a JavaScript array in the server-side code, which is used by the client-side validation code. 您可以使用许多附加的JavaScript验证框架,大多数常用的验证框架都可以通过在服务器端代码中生成类名称,特殊data-*属性或JavaScript数组的列表来工作。客户端验证代码使用。 The proper way to generate this validation data is in the controller, by asking the model for an array of the validations that it performs and then using that array inside the controller to build the JavaScript. 生成此验证数据的正确方法是在控制器中,方法是:向模型询问其执行的验证数组,然后在控制器内部使用该数组来构建JavaScript。

The only difference between this technique and the one described above is that the former only asks the model for the names of failed validations while the latter asks the model for the names of all validations. 该技术与上述技术之间的唯一区别在于,前者仅向模型询问失败的验证的名称,而后者则向模型询问所有验证的名称。

To do this, you will need to create a new class Validation extends Kohana_Validation and add a method to it get_rules that returns the validation rules for this object. 为此,您将需要创建一个新的类Validation extends Kohana_Validation并在其中添加一个方法get_rules来返回此对象的验证规则。 Then, in the controller, you can call my_model->validation()->get_rules() and process the array of rules to make the JavaScript. 然后,在控制器中,您可以调用my_model->validation()->get_rules()并处理规则数组以制作JavaScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM