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

发布于 2024-09-08 07:06:31 字数 1741 浏览 8 评论 0原文

我在 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)。

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

I've created a bunch of errors in a file under APPPATH/messages/validate.php with a bunch of common messages such as...

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.'
    ) 
);

This works great when I get errors like $errors = $post->errors('validate').

Is there a way to use these errors as base errors, and if I have a separate form which needs more, I can use a separate file with only the differences in it, for example it may look like

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

So obviously, any email error message will come from validate.php (inherited), but any permissions error will come from the new file with the error definition for permissions.

I named the file validate.php because the inherit behaviour seems to work with the system folder and that is what it is called under SYSPATH/messages/validate.php (see it on GitHub).

Can my error messages inherit from a base file, or should I just copy all the error messages per form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜唯美灬不弃 2024-09-15 07:06:31

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

common errors: 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.'
    )
);

specific errors: APPPATH/messages/specific.php

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

Kohana uses these sequence to find the message: APPPATH/messages/specific.php, APPPATH/messages/validate.php and SYSPATH/messages/validate.php

print_r(validate->errors('specific'));
迷迭香的记忆 2024-09-15 07:06:31

没有“黑客”:

    $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');

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

without "hacks":

    $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');

your model shouldn't validate your businesses logic.

屋檐 2024-09-15 07:06:31

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

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

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

Inheritance works automatically, follow this pattern:

  1. Search for a field+error specific message in the given file
  2. Search for a field+default message in the given file
  3. Search for a generic message in the given file
  4. Search for a generic message in validate file

So, if you overload the validate file and change the default messages, then inheritance will work as expected.

蓝色星空 2024-09-15 07:06:31

这很hacky,但是很有效!

$commonErrors = include APPPATH .  'messages/validate.php';

$errors =  array(
    'permission' => array(
        'not_empty' => 'You must give permission to continue.'
    )
);

return array_merge($commonErrors, $errors);

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

This is hacky, but it works!

$commonErrors = include APPPATH .  'messages/validate.php';

$errors =  array(
    'permission' => array(
        'not_empty' => 'You must give permission to continue.'
    )
);

return array_merge($commonErrors, $errors);

Basically, it auto inherits the base rules for you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文