我需要在 Kohana 3 的验证助手中添加一些错误。
这是我的开始:
// validate form
$post = Validate::factory($_POST)
// Trim all fields
->filter(TRUE, 'trim')
// Rules for name
->rule('first-name', 'not_empty')
->rule('last-name', 'not_empty')
// Rules for email address
->rule('email', 'not_empty')
->rule('email', 'email')
// Rules for address stuff
->rule('address', 'not_empty')
->rule('suburb', 'not_empty')
->rule('state', 'not_empty')
->rule('postcode', 'not_empty')
// Rules for misc
->rule('phone', 'not_empty')
->rule('company', 'not_empty')
->rule('abn', 'not_empty');
现在,我也检查了一些东西,如果遇到问题会添加错误
if ( ! in_array($post['state'], array_keys($states))) {
$post->error('state', 'not_found');
}
if ( $this->userModel->doesEmailExist($post['email'])) {
$post->error('email', 'already_exists');
}
我已经var_dump()
对这些做了一些,他们正在返回应该添加错误的值!
但是,当我调用时$post->check()
,它似乎只验证了我在上面第一个代码块中添加的规则。
我的 /application/messages/join.php 中也有匹配的值
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'not_empty' => ':field must not be empty.',
'matches' => ':field must be the same as :param1',
'regex' => ':field does not match the required format',
'exact_length' => ':field must be exactly :param1 characters long',
'min_length' => ':field must be at least :param1 characters long',
'max_length' => ':field must be less than :param1 characters long',
'in_array' => ':field must be one of the available options',
'digit' => ':field must be a digit',
'email' => array(
'email' => 'You must enter a valid email.',
'already_exists' => 'This email is already associated with an account'
),
'name' => 'You must enter your name.',
);
我在这里做错了吗?谢谢
更新
我只是在 Validation 库中做了一些快速调试,即_errors
在每次调用该error
方法后转储属性。
我可以看到,我的错误正在被添加,但随后被覆盖(可能与我在上面添加的规则相冲突)。这是正常的吗?