Kohana orm 验证

发布于 2024-12-26 19:58:17 字数 353 浏览 0 评论 0 原文

我有完全有效的验证脚本,我的问题是我无法获取自定义错误消息

这是我的注册函数: http:// astebin.com/ZF3UVxUr

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

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

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

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

And here is my message array: http://pastebin.com/d9GUvM3N

my messages script is in: \application\messages\registration.php Any suggestions?

Sorry about long code just skip html and other stuff

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

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

发布评论

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

评论(2

画▽骨i 2025-01-02 19:58:17

如果您捕获用户模型引发的验证异常,则您的消息文件位置可能不正确。它需要是:“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');
}

If you're catching the validation exception that is thrown by the User model, then likely your messages file location is incorrect. It needs to be: '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. 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');
}
当爱已成负担 2025-01-02 19:58:17

在尝试使用任何模型之前,您应该验证发布数据。您的验证规则未执行,因为您尚未执行验证检查()

我会做类似的事情:

// ./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.
)

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().

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:

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