编程语言
首页 > 编程语言> > php – Kohana 3的验证错误可以继承吗?

php – Kohana 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',
);

很明显,任何电子邮件错误消息都来自validate.php(继承),但任何权限错误都将来自具有权限错误定义的新文件.

我将文件命名为validate.php,因为继承行为似乎与系统文件夹一起使用,这就是在SYSPATH / messages / validate.php下调用它(在GitHub上查看).

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

解决方法:

继承自动运行,遵循以下模式:

>在给定文件中搜索特定于字段错误的消息
>在给定文件中搜索字段默认消息
>在给定文件中搜索通用消息
>在验证文件中搜索通用消息

因此,如果重载验证文件并更改默认消息,则继承将按预期工作.

标签:kohana,kohana-3,php,validation
来源: https://codeday.me/bug/20190724/1518356.html