验证错误消息

发布于 2024-11-13 00:45:18 字数 950 浏览 1 评论 0原文

我正在为 Kohana 开发授权系统。我这样做只是为了教育...

这就是我的控制器检查提交字段的样子:

$validation =
  Validation::factory( $_POST )
    ->rule( 'username', 'not_empty' )
    ->rule( 'username', 'max_length', array( ':value', 32 ) )
    ->rule( 'username', 'alpha_dash', array( ':value', true ) )
    ->rule( 'password', 'not_empty' )
    ->rule( 'password', 'min_length', array( ':value', 6 ) )
    ->rule( 'password', 'max_length', array( ':value', 255 ) )
    ->rule( 'passwordRepeatedly', 'not_empty' )
    ->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
    ->rule( 'email', 'not_empty' )
    ->rule( 'email', 'email' );

我正在寻找为每个添加的规则显示不同错误消息的方法。我的目标是传递它(一个或全部(如果发生))以在那里查看和显示它们。

伪代码:

errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';

如何为每个规则定义不同的错误?我在文档中找不到任何对我来说可以理解的东西......

I'm working on authorization system for Kohana. I'm doing it just for education...

This is how looks my controller that checks submitted fields:

$validation =
  Validation::factory( $_POST )
    ->rule( 'username', 'not_empty' )
    ->rule( 'username', 'max_length', array( ':value', 32 ) )
    ->rule( 'username', 'alpha_dash', array( ':value', true ) )
    ->rule( 'password', 'not_empty' )
    ->rule( 'password', 'min_length', array( ':value', 6 ) )
    ->rule( 'password', 'max_length', array( ':value', 255 ) )
    ->rule( 'passwordRepeatedly', 'not_empty' )
    ->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
    ->rule( 'email', 'not_empty' )
    ->rule( 'email', 'email' );

I'm looking for the way to display different error message for each added rule. My goal is then pass it (one or all (if occurs)) to view and display them there.

Pseudo-code:

errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';

How to define different error for each rule? I can't find anything understandable for me in the docs...

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

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

发布评论

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

评论(2

岁月打碎记忆 2024-11-20 00:45:18

你有:

$validation = ...

所以,首先你应该检查变量是否通过验证:

if($validation->check()) 
{
  // no errors
}
else
{
    $errors = $validation->errors('user');
}

然后你应该在 application/messages 中有 user.php 文件

<?php defined('SYSPATH') or die('No direct script access.');

  return array
  (
     'input_name' => array
     (
        'rule' => 'your message',
        'default' => 'default message'
     ),
     'username' => array
     (
        'not_empty' => 'your message',
        'max_length' => 'your message',
        'alpha_dash' => 'your message',
        'default' => 'default message'
     ),

  );

?>

来显示错误:

foreach($errors as $input_field => $message) 
    echo $message;

You have:

$validation = ...

So, first you should check if variables pass validation:

if($validation->check()) 
{
  // no errors
}
else
{
    $errors = $validation->errors('user');
}

Then you should have user.php file in application/messages

<?php defined('SYSPATH') or die('No direct script access.');

  return array
  (
     'input_name' => array
     (
        'rule' => 'your message',
        'default' => 'default message'
     ),
     'username' => array
     (
        'not_empty' => 'your message',
        'max_length' => 'your message',
        'alpha_dash' => 'your message',
        'default' => 'default message'
     ),

  );

?>

To display errors:

foreach($errors as $input_field => $message) 
    echo $message;
飘过的浮云 2024-11-20 00:45:18

验证规则使用应用程序目录中的messages文件夹。

检查这个:http://kohanaframework.org/3.1/guide/orm/examples/validation< /a> 用于完整验证示例,其中消息文件位于页面的最底部。

需要注意的是消息文件的目录文件名

在 KO3.1 中(我相信),只要失败,验证就会抛出异常。如果您使用 catch (ORM_Validation_Exception $e) 捕获它,您可以使用 $e->errors('some_directory') 来捕获错误消息,然后从 < code>messages/some_directory/model_name.php 以数组的形式,就像上面链接的示例一样。

Validation rules make use of messages folder in your application directory.

Check this: http://kohanaframework.org/3.1/guide/orm/examples/validation for full validation example, where the messages file is at the very bottom of the page.

The things to note are the directory and the filename of the message file.

In KO3.1 (I believe) the Validation throws an exception whenever if fails. If you catch it with catch (ORM_Validation_Exception $e) you can use $e->errors('some_directory') to catch error messages, which are then pulled from messages/some_directory/model_name.php in the form of array, just like in the example from the link above.

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