php - Kohana 3 的验证错误可以继承吗?

标签 php validation kohana 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',
);

很明显,任何 email 错误消息都将来自 validate.php(继承),但任何 permissions 错误都将来自新的包含权限错误定义的文件。

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

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

最佳答案

常见错误: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'));

关于php - Kohana 3 的验证错误可以继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3191176/

相关文章:

PHP 基本电子邮件表单,尽管回复有效,但所有消息均来自(未知)

javascript - 仅包含数字和破折号的正则表达式

routes - 如何在 Kohana 中的 Controller 名称之间添加破折号?

php - HTML 选择类型

php - 使用 CloudFlare 和 OpenShift 以及 WordPress 应用程序的重定向循环

php - Mysqli 插入语句

php - 验证为表单的属性

python - Python 中永无休止的验证检查

php - 无法删除字符串中的空格

php - Kohana:有哪些替代方案?