尝试创建用户时处理验证异常(Kohana 3.1 ORM / Auth模块)

时间:2011-03-25 17:08:20

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

将Kohana 3.1与Auth ORM模块一起使用,如果我使用create_user创建新用户,如何处理验证异常并为页面中的每个显示错误?在这种情况下,密码为短(<8个字符),但也可能是password_confirmpassword不匹配。

$user = ORM::factory('user')
        ->where('username', '=', 'admin')->find();
if( ! $user->loaded())
{
    $this->template->content = __('Admin user does not exist. Creating...');

    $user = ORM::factory('user');
    $user->create_user(
        array(
         'username' => 'admin',
         'email' => 'admin@example.com',
         'password' => 'admin',
         'password_confirm' => 'admin'
        ),
        array(
         'username',
         'email',
         'password'
    ));

    // remember to add the login role AND the admin role
    // add a role; add() executes the query immediately
    $user->add('roles', ORM::factory('role')->where('name', '=', 'login')->find());
    $user->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find());
}

1 个答案:

答案 0 :(得分:3)

希望你已经找到了问题的答案,但无论如何:

try
{
    $user->create_user(array(..))
}
catch (ORM_Validation_Exception $e)
{
    $validation_errors = $e->errors(''); // an array of errors will be stored in $validation_errors
}