Kohana 3.2 一个模型或字段的自定义验证错误消息
我的一般验证消息位于 application/messages/validation.php 中,当我验证用户模型的“密码”字段时,我需要自定义消息。这是我的初始代码:
try
{
ORM::factory('user', Auth::instance()->get_user())->update_user($values);
}
catch (ORM_Validation_Exception $e)
{
$errors = Arr::merge($errors, $e->errors(TRUE));
if (Arr::get($errors, '_external'))
{
$errors = Arr::merge($errors, Arr::get($errors, '_external'));
unset($errors['_external']);
}
}
我尝试过:$e->errors('')
,$e->errors()
,复制application/ messages/validation.php
到 application/messages/user.php
,将 'password' =>; array('regex' => 'message')
在这些文件中,我尝试混合所有这些的多种可能性。我阅读了异常类的源代码,但我不明白出了什么问题。根据这篇帖子,它可以完成,但对我来说没有用。 我很感激帮助。谢谢!
I have my general validation messages in application/messages/validation.php
and I need a custom message when I'm validating the 'password' field for my User model. This is my initial code:
try
{
ORM::factory('user', Auth::instance()->get_user())->update_user($values);
}
catch (ORM_Validation_Exception $e)
{
$errors = Arr::merge($errors, $e->errors(TRUE));
if (Arr::get($errors, '_external'))
{
$errors = Arr::merge($errors, Arr::get($errors, '_external'));
unset($errors['_external']);
}
}
And I tried: $e->errors('')
, $e->errors()
, copying application/messages/validation.php
to application/messages/user.php
, putting 'password' => array('regex' => 'message')
inside of those files, I tried mixing many possibilities of all this. I read the source code for the exception class, but I can't understand what's wrong. According to this post, it can be done, but didn't worked for me that way.
I appreciate help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为
ORM_Validation_Exception->generate_errors()
中存在错误。您尝试做的事情应该像调用$e->errors('validation')
一样简单,并且消息将来自application/validation/user.php
(它附加 ORM 模型别名)。我还没有报告它,但我认为这个错误报告无论如何都涉及到它。希望它能得到修复。
在 modules/orm/classes/kohana/orm/validation/exception.php 中,将第 153 行更改为 $errors[$key] = $this->generate_errors(< strong>
$alias
, $object, $directory, $translate);
希望这有帮助
I think there is a bug in
ORM_Validation_Exception->generate_errors()
. What you are trying to do should be as simple as calling$e->errors('validation')
, and messages would come fromapplication/validation/user.php
(it appends the ORM model alias).I haven't reported it yet, but I think this bug report touches on it anyways. Hopefully it gets fixed.
In
modules/orm/classes/kohana/orm/validation/exception.php
, change line 153 to$errors[$key] = $this->generate_errors(
$alias
, $object, $directory, $translate);
Hope this helps
如果有验证功能,您可以在 APPPATH/messages/validation.php 中尝试。
If there's a validation function you could try this in APPPATH/messages/validation.php.