如何通过模型标签和错误消息传递外部验证



在Kohana 3.2中,在保存时通过Model_User的外部验证,为什么不会显示正确的消息?

我在application/messages/models中有user.php,它读取和翻译"内部"数据很好,而_external.php位于application/messages/models/user中。

当_external data无效时,将显示来自Kohana的默认错误消息,因此没有正确翻译或给出来自Model_User的正确标签。

编辑,代码:
// We have $_POST, register a new user
$user = ORM::factory('user');
/*
 *    Here a bunch of variables are set
 */
$extra = Validation::factory($_POST)->
        rule('email', 'email')-> // I run this check, because in my Model_User, email is filtered through Encrypt
        rule('name', 'not_empty'); // Same goes for name
try {
    $user->save($extra);
} catch (ORM_Validation_Exception $e) {
    $this->template->errors = $e->errors('models', true);
}

因此,当$extra变量与规则不匹配时,我希望从application/messages/models/user/_external.php获得不错的错误消息,看起来像:

return array(
    'email' => array(
            'email'         => ':field must be a valid email address',
        ),
    'name' => array(
            'not_empty'     => ':field must not be empty',
        ),
);

另外,如果:字段是从Model_User "labels"中获取的,那就太好了。

您需要将_external.php放在messages/models目录中的user.php旁边,而不是messages/models/user目录中。

最新更新