Kohana 3.2 一个模型或字段的自定义验证错误消息

发布于 2024-12-26 16:47:01 字数 889 浏览 1 评论 0原文

我的一般验证消息位于 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.phpapplication/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 技术交流群。

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

发布评论

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

评论(2

此生挚爱伱 2025-01-02 16:47:01

我认为 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 from application/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

风筝有风,海豚有海 2025-01-02 16:47:01

如果有验证功能,您可以在 APPPATH/messages/validation.php 中尝试。

return array(
   'password' => 'your password text' // this way it will always be replaced
   'Model_User::password' => 'your custom password message' // And here just for the Model_User
);

If there's a validation function you could try this in APPPATH/messages/validation.php.

return array(
   'password' => 'your password text' // this way it will always be replaced
   'Model_User::password' => 'your custom password message' // And here just for the Model_User
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文