我在APPPATH/messages/validate.php
下的文件中创建了一堆错误,其中包含一些常见的消息,例如......
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' => 'You must enter a valid email.',
'name' => 'You must enter your name.',
'enquiry' => 'You must enter your enquiry.',
'captcha' => array (
'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',
'not_empty' => 'You must enter the characters from the image.'
)
);
当我收到$errors = $post->errors('validate')
等错误时,这很有用。
有没有办法将这些错误用作 base 错误,如果我有一个需要更多的单独表单,我可以使用一个单独的文件,只有它的差异,例如它可能看起来像
return array(
'permissions' => 'Please agree to the permissions',
);
很明显,任何email
错误消息都来自validate.php
(继承),但任何permissions
错误都来自新文件,其中包含permissions
的错误定义
我将文件命名为validate.php
,因为继承行为似乎与system
文件夹一起使用,这就是SYSPATH/messages/validate.php
下的调用(在GitHub上查看)
我的错误消息是否可以从基本文件继承,还是应该只复制每个表单的所有错误消息?
答案 0 :(得分:4)
常见错误:APPPATH / messages / validate.php
return array(
'email' => 'You must enter a valid email.',
'name' => 'You must enter your name.',
'enquiry' => 'You must enter your enquiry.',
'captcha' => array (
'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',
'not_empty' => 'You must enter the characters from the image.'
)
);
具体错误:APPPATH / messages / specific.php
return array(
'permissions' => 'Please agree to the permissions',
);
Kohana使用这些序列来查找消息:APPPATH / messages / specific.php,APPPATH / messages / validate.php和SYSPATH / messages / validate.php
print_r(validate->errors('specific'));
答案 1 :(得分:3)
没有“黑客”:
$orm->values($form[$this->name])->check();
$not_model_errors = Validate::factory(array())->rule(NULL, 'permissions_rules'); // doesn't matter what args you send here, just meet the vartype
// add test error
$not_model_errors->error(NULL, 'test_error', array());
$this->template->errors = $orm->validate()->errors('validation') + $not_model_errors->errors('permissions');
您的模型不应验证您的业务逻辑。
答案 2 :(得分:2)
继承自动运行,遵循以下模式:
validate
档案因此,如果重载validate
文件并更改默认消息,则继承将按预期工作。
答案 3 :(得分:0)
这很hacky,但它确实有效!
$commonErrors = include APPPATH . 'messages/validate.php';
$errors = array(
'permission' => array(
'not_empty' => 'You must give permission to continue.'
)
);
return array_merge($commonErrors, $errors);
基本上,它会自动为您继承基本规则!