我需要向Kohana3的验证助手添加一些错误。
以下是我的出发点:
// 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.',
);
我在这里做错什么了吗?谢谢
更新
我刚刚在验证库中做了一些快速调试,即转储
_errors
每次调用后的属性
error
方法。
我能看到的是,我的错误正在被添加,但随后被覆盖(可能与我上面添加的规则相冲突)。这正常吗?