我正在为 Kohana 开发授权系统。我这样做只是为了教育......
这是我的 Controller 检查提交字段的样子:
$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...';
如何为每个规则定义不同的错误?我在文档中找不到任何对我来说可以理解的内容...
最佳答案
你有:
$validation = ...
所以,首先你应该检查变量是否通过验证:
if($validation->check())
{
// no errors
}
else
{
$errors = $validation->errors('user');
}
然后你应该在 application/messages 中有 user.php 文件
<?php defined('SYSPATH') or die('No direct script access.');
return array
(
'input_name' => array
(
'rule' => 'your message',
'default' => 'default message'
),
'username' => array
(
'not_empty' => 'your message',
'max_length' => 'your message',
'alpha_dash' => 'your message',
'default' => 'default message'
),
);
?>
显示错误:
foreach($errors as $input_field => $message)
echo $message;
关于php - 验证错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6217221/