简体   繁体   English

Kohana orm验证

[英]Kohana orm validation

I have fully working validation script my problem is that i can't get custom error messages 我的验证脚本运行正常,我的问题是我无法获得自定义错误消息

Here is my function for registration: http://pastebin.com/ZF3UVxUr 这是我的注册功能: http : //pastebin.com/ZF3UVxUr

And here is my message array: http://pastebin.com/d9GUvM3N 这是我的消息数组: http : //pastebin.com/d9GUvM3N

my messages script is in: \\application\\messages\\registration.php Any suggestions? 我的消息脚本位于: \\application\\messages\\registration.php什么建议吗?

Sorry about long code just skip html and other stuff 抱歉,长代码只是跳过html和其他内容

If you're catching the validation exception that is thrown by the User model, then likely your messages file location is incorrect. 如果您正在捕获User模型引发的验证异常,则可能是您的消息文件位置不正确。 It needs to be: 'registration/user.php'. 它必须是:“ 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'
    )
);

Also, contrary to Michael P's response, you should store all validation logic in the model. 另外,与Michael P的回答相反,您应该将所有验证逻辑存储在模型中。 Your controller code, to register a new user, should be as simple as: 要注册新用户,您的控制器代码应该很简单:

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');
}

You should be validating the post data before attempting to hit any models. 在尝试使用任何模型之前,您应该先验证发布数据。 Your validation rules are not being executed because you haven't performed a validation check() . 由于尚未执行验证check(),因此未执行验证规则。

I would do something like: 我会做类似的事情:

// ./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 stays mostly the same, with the exception of fixing up the 'lenght' spelling mistake you had: 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'
    )
);

Then, sending an empty 'name' field will return: 然后,发送一个空的“名称”字段将返回:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM