doufan9377 2010-07-07 00:43
浏览 31
已采纳

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

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?

  • 写回答

4条回答 默认 最新

  • duanhu7615 2010-07-21 14:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向