3

我在一个文件中创建了一堆错误,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').

有没有办法将这些错误用作基本错误,如果我有一个需要更多的单独表单,我可以使用一个单独的文件,其中只有其中的差异,例如它可能看起来像

return array(
    'permissions'    => 'Please agree to the permissions',
);

所以很明显,任何email错误消息都将来自validate.php(继承),但任何permissions错误都将来自带有错误定义的新文件permissions

我命名该文件validate.php是因为继承行为似乎适用于该system文件夹,这就是它在下面的名称SYSPATH/messages/validate.php(参见GitHub 上的内容)。

我的错误消息可以继承自基本文件,还是应该只复制每个表单的所有错误消息?

4

4 回答 4

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'));
于 2010-08-20T18:15:04.470 回答
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');

您的模型不应该验证您的业务逻辑。

于 2010-07-07T10:40:33.550 回答
2

继承自动工作,遵循以下模式:

  1. 在给定文件中搜索字段+错误特定消息
  2. 在给定文件中搜索字段+默认消息
  3. 在给定文件中搜索通用消息
  4. validate在文件中搜索通用消息

因此,如果您重载validate文件并更改默认消息,那么继承将按预期工作。

于 2010-07-21T14:36:13.077 回答
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);

基本上,它会自动为您继承基本规则!

于 2010-07-07T01:11:20.413 回答