验证错误消息
我正在为 Kohana 开发授权系统。我这样做只是为了教育...
这就是我的控制器检查提交字段的样子:
$validation =
Validation::factory( $_POST )
->rule( 'username', 'not_empty' )
->rule( 'username', 'max_length', array( ':value', 32 ) )
->rule( 'username', 'alpha_dash', array( ':value', true ) )
->rule( 'password', 'not_empty' )
->rule( 'password', 'min_length', array( ':value', 6 ) )
->rule( 'password', 'max_length', array( ':value', 255 ) )
->rule( 'passwordRepeatedly', 'not_empty' )
->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
->rule( 'email', 'not_empty' )
->rule( 'email', 'email' );
我正在寻找为每个添加的规则显示不同错误消息的方法。我的目标是传递它(一个或全部(如果发生))以在那里查看和显示它们。
伪代码:
errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';
如何为每个规则定义不同的错误?我在文档中找不到任何对我来说可以理解的东西......
I'm working on authorization system for Kohana. I'm doing it just for education...
This is how looks my controller that checks submitted fields:
$validation =
Validation::factory( $_POST )
->rule( 'username', 'not_empty' )
->rule( 'username', 'max_length', array( ':value', 32 ) )
->rule( 'username', 'alpha_dash', array( ':value', true ) )
->rule( 'password', 'not_empty' )
->rule( 'password', 'min_length', array( ':value', 6 ) )
->rule( 'password', 'max_length', array( ':value', 255 ) )
->rule( 'passwordRepeatedly', 'not_empty' )
->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
->rule( 'email', 'not_empty' )
->rule( 'email', 'email' );
I'm looking for the way to display different error message for each added rule. My goal is then pass it (one or all (if occurs)) to view and display them there.
Pseudo-code:
errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';
How to define different error for each rule? I can't find anything understandable for me in the docs...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有:
所以,首先你应该检查变量是否通过验证:
然后你应该在 application/messages 中有 user.php 文件
来显示错误:
You have:
So, first you should check if variables pass validation:
Then you should have user.php file in application/messages
To display errors:
验证规则使用应用程序目录中的messages文件夹。
检查这个:http://kohanaframework.org/3.1/guide/orm/examples/validation< /a> 用于完整验证示例,其中消息文件位于页面的最底部。
需要注意的是消息文件的目录和文件名。
在 KO3.1 中(我相信),只要失败,验证就会抛出异常。如果您使用
catch (ORM_Validation_Exception $e)
捕获它,您可以使用$e->errors('some_directory')
来捕获错误消息,然后从 < code>messages/some_directory/model_name.php 以数组的形式,就像上面链接的示例一样。Validation rules make use of messages folder in your application directory.
Check this: http://kohanaframework.org/3.1/guide/orm/examples/validation for full validation example, where the messages file is at the very bottom of the page.
The things to note are the directory and the filename of the message file.
In KO3.1 (I believe) the Validation throws an exception whenever if fails. If you catch it with
catch (ORM_Validation_Exception $e)
you can use$e->errors('some_directory')
to catch error messages, which are then pulled frommessages/some_directory/model_name.php
in the form of array, just like in the example from the link above.