jnbdz
jnbdz

Reputation: 4383

Problem with Kohana 3.2 Validation class. It seems it is not sending Exception when there is an error

When the POST data is send to:

Auth::instance()->register( $_POST );

The data is then validated... If it's not I assume the Validation Class in Kohana throws an Exception. And then try-catch function catches it. The problem I am having is with this method:

$this->send_confirmation_email($_POST);

It is called even if the data is not valid. I believed that if the data was not valid it would skip everything else and jump to catch... But it seems I was wrong because I am getting a nasty Fatal error from the method to send an email because it cannot find the email address...

try {
        if( ! $optional_checks ) {
           //throw new ORM_Validation_Exception("Invalid option checks");
        }

        $_POST['email_code'] = Auth::instance()->hash(date('YmdHis', time()));

        Auth::instance()->register( $_POST );

        $this->send_confirmation_email($_POST);

        // sign the user in
        Auth::instance()->login($_POST['username'], $_POST['password']);

        // redirect to the user account
        $this->request->redirect('user/profile');
     } catch (Validation_Exception $e) {

So, is there a way so that the method to send an email is skip if the data is not valid?

One could say I should use check() method. Here is why it is a problem:

 Validation::factory($fields)
                    ->rules('username', $this->_rules['username'])
                    ->rule('username', 'username_available', array($this, ':field'))
                    ->rules('email', $this->_rules['email'])
                    ->rule('email', 'email_available', array($this, ':field'))
                    ->rules('password', $this->_rules['password'])
                    ->rules('password_confirm', $this->_rules['password_confirm']);

            if (Kohana::config('useradmin')->activation_code) {
                    Validation::factory($fields)->rule('activation_code', 'check_activation_code', array($this, ':field'));
            }

Thanks in advance for any help.

UPDATE:

It seems now that there is a problem with Kohana Validation class.

Here is the method in my Model_User class:

public function create_user($fields)
{
    Validation::factory($fields)
        ->rules('username', $this->_rules['username'])
        ->rule('username', 'username_available', array($this, ':field'))
        ->rules('email', $this->_rules['email'])
        ->rule('email', 'email_available', array($this, ':field'))
        ->rules('password', $this->_rules['password'])
        ->rules('password_confirm', $this->_rules['password_confirm']);
        //->labels($_labels);

    if (Kohana::config('useradmin')->activation_code) {
        Validation::factory($fields)->rule('activation_code', 'check_activation_code', array($this, ':field'));
    }

    // Generate a unique ID
    $uuid = CASSANDRA::Util()->uuid1();

    //CASSANDRA::selectColumnFamily('UsersRoles')->insert($username, array('rolename' => 'login'));
    CASSANDRA::selectColumnFamily('Users')->insert($uuid, array(
                            'username'      => $fields['username'],
                            'email'         => $fields['email'],
                            'password'      => Auth::instance()->hash($fields['password']),
                            'logins'        => 0,
                            'last_login'        => 0,
                            'last_failed_login' => 0,
                            'failed_login_count'    => 0,
                            'created'       => date('YmdHis', time()),
                            'modify'        => 0,
                            'role'          => 'login',
                            'email_verified'    => $fields['email_code'],
                        ));
}

The code after the Validation class is executed. So even when the data is not valid it still adds a new user to the database.

The test I am doing is with empty inputs.

Here are the rules:

protected $_rules = array(
    'username' => array(
        'not_empty' => NULL,
        'min_length' => array(4),
        'max_length' => array(32),
        'regex' => array('/^[-\pL\pN_.]++$/uD'),    
    ),
    'password' => array(
        'not_empty' => NULL,
        'min_length' => array(8),
        'max_length' => array(42),
    ),
    'password_confirm' => array(
        'matches' => array('password'),
    ),
    'email' => array(
        'not_empty' => NULL,
        'min_length' => array(4),
        'max_length' => array(127),
        'validate::email' => NULL,
    ),
);

Thanks again in advance for any help.

Upvotes: 0

Views: 3191

Answers (3)

jnbdz
jnbdz

Reputation: 4383

The way I setup the rules was wrong and the way I setup the Validation custom rules. Here is the code that resolved the problem:

protected $_rules = array(
    'username' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(32)),
        array('regex', array('/^[-\pL\pN_.]++$/uD')),
    ),
    'password' => array(
        array('not_empty'),
        array('min_length', array(8)),
        array('max_length', array(42)),
    ),
    'password_confirm' => array(
        array('matches', array(':validation', ':field', 'password')),
    ),
    'email' => array(
        array('not_empty'),
        array('min_length', array(4)),
        array('max_length', array(127)),
        array('email'),
    ),
);

Validation:

$validation = Validation::factory($fields)
        ->rules('username', $this->_rules['username'])
        ->rule('username', array($this, 'username_available'), array(':validation', ':field'))
        ->rules('email', $this->_rules['email'])
        ->rule('email', array($this, 'email_available'), array(':validation', ':field'))
        ->rules('password', $this->_rules['password'])
        ->rules('password_confirm', $this->_rules['password_confirm'])
        ->errors('register/user');
        //->labels($_labels);

    if (Kohana::config('useradmin')->activation_code) {
        $validation->rule('activation_code', array($this, 'check_activation_code'), array(':validation', ':field'));
    }

    if(!$validation->check())
    {
        throw new Validation_Exception($validation, __('Your registering information is not valid.'));
    }

Thank you all for your support!

Upvotes: 2

Gabriel Gartz
Gabriel Gartz

Reputation: 2870

In this case, the Kohana Auth class dons't have the method register() as default.

I have implemented this method to use in a specific case, with AmfPHP gateway for Kohana Useradmin module.

The right way is to use the model method to add user and it roles, you will get a throw if the array with fields isn't valid.

You can get the result from Auth->instance()->register() like:

    if(! Auth::instance()->register( $_POST )) throw new Validation_Exception(...);

Try or the best way is to change the Auth->instance()->register($_POST) to:

    $user->create_user($_POST, array(
        'username',
        'password',
        'email',
    ));
    // Add the login role to the user (add a row to the db)
    $login_role = new Model_Role(array('name' =>'login'));
    $user->add('roles', $login_role);

It will make your try catch work as needed for Kohana_Validation exception.

To finish, here is the code from register method, it could help you:

/**
 * Register a single user
 * Method to register new user by Useradmin Auth module, when you set the
 * fields, be sure they must respect the driver rules
 * 
 * @param array $fields An array witch contains the fields to be populate
 * @returnboolean Operation final status
 * @see Useradmin_Driver_iAuth::register()
 */
public function register($fields) 
{
    if( ! is_object($fields) ) 
    {
        // Load the user
        $user = ORM::factory('user');
    } 
    else 
    {
        // Check for instanced model
        if( $fields instanceof Model_User ) 
        {
            $user = $fields;
        } 
        else 
        { 
            throw new Kohana_Exception('Invalid user fields.');
        }
    }
    try 
    {
        $user->create_user($fields, array(
            'username',
            'password',
            'email',
        ));
        // Add the login role to the user (add a row to the db)
        $login_role = new Model_Role(array('name' =>'login'));
        $user->add('roles', $login_role);
    } 
    catch (ORM_Validation_Exception $e) 
    {
        throw $e;
        return FALSE;
    }
    return TRUE;
}

Upvotes: 1

Jani Hartikainen
Jani Hartikainen

Reputation: 43243

There's this thing called an if-else you could use... ;)

Your idea about how the try-catch works is correct: When an exception is thrown inside the try-block, all the remaining code in it is skipped, and it jumps directly into the catch-block.

Most likely the register function is simply not throwing an exception like you assumed, which would be the reason your code is not doing what you think it should.

Upvotes: 1

Related Questions