【问题标题】:Custom validation error messages in Kohana 3.0Kohana 3.0 中的自定义验证错误消息
【发布时间】:2011-09-28 23:33:59
【问题描述】:

我正在尝试加载自定义错误消息以在验证我的“注册”表单时使用。

用户模型:

https://github.com/ashleyconnor/Egotist/blob/master/classes/model/user.php

帐户管理员:

https://github.com/ashleyconnor/Egotist/blob/master/classes/controller/user/account.php

注册视图:

https://github.com/ashleyconnor/Egotist/blob/master/views/account/signup.php

然后我将user.php 放入/messages/models/user.php,但我的新错误消息没有显示在表单中。

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

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

?>

https://github.com/ashleyconnor/Egotist/blob/master/messages/models/user.php

所以提交一个空表单会给出以下错误信息:

  • 用户名不能为空
  • 电子邮件地址不能为空
  • 密码不能为空

哪些是默认的。

【问题讨论】:

    标签: validation kohana kohana-3


    【解决方案1】:

    来自KohanaValidation类,errors方法源码:

     * Returns the error messages. If no file is specified, the error message
     * will be the name of the rule that failed. When a file is specified, the
     * message will be loaded from "field/rule", or if no rule-specific message
     * exists, "field/default" will be used. If neither is set, the returned
     * message will be "file/field/rule".
     *
     * By default all messages are translated using the default language.
     * A string can be used as the second parameter to specified the language
     * that the message was written in.
     *
     *     // Get errors from messages/forms/login.php
     *     $errors = $Validation->errors('forms/login');  
    

    尝试使用$errors = $post-&gt;errors('models/user') 代替$errors = $post-&gt;errors('signup')

    【讨论】:

    • 此外,控制器的第 26 行出现错误:$errors = $_POST-&gt;errors('login'); 它正在尝试调用 $_POST 数组上的 errors() 方法 - 而不是 Validation 类的实例。
    • 实际上,当我将其修改为 $post 时,它破坏了我的登录验证。这相当于 $_POST。
    猜你喜欢
    • 2016-12-04
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2013-04-08
    相关资源
    最近更新 更多