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)。
我的错误消息可以从基本文件继承吗?或者我应该复制每个表单的所有错误消息?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
常见错误:APPPATH/messages/validate.php
特定错误:APPPATH/messages/specific.php
Kohana 使用以下序列查找消息:APPPATH/messages/specific.php、APPPATH/messages/validate.php 和 SYSPATH/messages/validate .php
common errors: APPPATH/messages/validate.php
specific errors: APPPATH/messages/specific.php
Kohana uses these sequence to find the message: APPPATH/messages/specific.php, APPPATH/messages/validate.php and SYSPATH/messages/validate.php
没有“黑客”:
您的模型不应验证您的业务逻辑。
without "hacks":
your model shouldn't validate your businesses logic.
继承自动工作,遵循以下模式:
validate 中搜索通用消息
文件因此,如果您重载
validate
文件并更改默认消息,那么继承将按预期工作。Inheritance works automatically, follow this pattern:
validate
fileSo, if you overload the
validate
file and change the default messages, then inheritance will work as expected.这很hacky,但是很有效!
基本上,它会自动为您继承基本规则!
This is hacky, but it works!
Basically, it auto inherits the base rules for you!