【问题标题】:Kohana orm validationKohana orm 验证
【发布时间】:2012-01-13 19:20:57
【问题描述】:

我有完整的验证脚本我的问题是我无法获得自定义错误消息

这是我的注册功能:http://pastebin.com/ZF3UVxUr

这是我的消息数组:http://pastebin.com/d9GUvM3N

我的消息脚本在:\application\messages\registration.php 有什么建议吗?

抱歉,代码太长,请跳过 html 和其他内容

【问题讨论】:

    标签: kohana kohana-3 kohana-orm kohana-auth


    【解决方案1】:

    如果您正在捕获 User 模型引发的验证异常,那么您的消息文件位置可能不正确。它必须是:'registration/user.php'。

    // ./application/messages/registration/user.php
    return array(
        'name' => array(
            'not_empty' => 'Please enter your username.',
        ),
        'password' => array(
            'matches' => 'Passwords doesn\'t match',
            'not_empty' => 'Please enter your password'
        ),
        'email' => array(
            'email' => 'Your email isn\'t valid',
            'not_empty' => 'Please enter your email'
        ),
        'about-me' => array(
            'max_length' => 'You cann\'ot exceed 300 characters limit'
        ),
        '_external' => array(
            'username' => 'This username already exist'
        )
    );
    

    此外,与 Michael P 的回应相反,您应该将所有验证逻辑存储在模型中。注册新用户的控制器代码应该很简单:

    try
    {
      $user->register($this->request->post());
    
      Auth::instance()->login($this->request->post('username'), $this->request->post('password'));
    }
    catch(ORM_Validation_Exception $e) 
    {
      $errors = $e->errors('registration');
    }
    

    【讨论】:

    • 谢谢你解决了我的问题,但由于某种原因_external' doesn't work,它给了我registration/user.username.unique insted 我想要得到的正常消息
    • 我认为外部消息需要在不同的文件中:./application/messages/registration/_external.php
    • 我发现在核心验证类的errors()方法中检查消息文件路径很有用。
    【解决方案2】:

    您应该在尝试点击任何模型之前验证发布数据。你的验证规则没有被执行,因为你没有执行validation check()

    我会这样做:

    // ./application/classes/controller/user
    class Controller_User extends Controller
    {
    
        public function action_register()
        {
    
            if (isset($_POST) AND Valid::not_empty($_POST)) {
                $post = Validation::factory($_POST)
                    ->rule('name', 'not_empty');
    
                if ($post->check()) {
                    try {
                        echo 'Success';
                        /**
                        * Post is successfully validated, do ORM
                        * stuff here
                        */
                    } catch (ORM_Validation_Exception $e) {
                        /**
                        * Do ORM validation exception stuff here
                        */
                    }
                } else {
                    /**
                    * $post->check() failed, show the errors
                    */
                    $errors = $post->errors('registration');
    
                    print '<pre>';
                    print_r($errors);
                    print '</pre>';
                }
            }
        }
    }
    

    Registration.php 基本保持不变,只是修正了您遇到的“长度”拼写错误:

    // ./application/messages/registration.php
    return array(
        'name' => array(
            'not_empty' => 'Please enter your username.',
        ),
        'password' => array(
            'matches' => 'Passwords doesn\'t match',
            'not_empty' => 'Please enter your password'
        ),
        'email' => array(
            'email' => 'Your email isn\'t valid',
            'not_empty' => 'Please enter your email'
        ),
        'about-me' => array(
            'max_length' => 'You cann\'ot exceed 300 characters limit'
        ),
        '_external' => array(
            'username' => 'This username already exist'
        )
    );
    

    然后,发送一个空的“名称”字段将返回:

    Array
    (
        [name] => Please enter your username.
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多