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.
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:
Validation:
Thank you all for your support!