SamWM
SamWM

Reputation: 5356

Handling validation exceptions when trying to create a user (Kohana 3.1 ORM / Auth module)

Using Kohana 3.1 with the Auth ORM module, if I create a new user using create_user, how can I handle validation exceptions and display an error for each one in the page? In this case, the password is to short (< 8 characters), but it could also be that password_confirm does not match password.

$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' => '[email protected]',
         '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());
}

Upvotes: 1

Views: 1580

Answers (1)

Chvanikoff
Chvanikoff

Reputation: 1329

Hope you've already found an answer to your question but anyway:

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

Upvotes: 3

Related Questions