Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are assigning <code>$username</code> and <code>$password</code> before running the validation. <code>set_rules</code> method just sets the rules for validation. It doesn't run it. They are validated at <code>$this-&gt;form_validation-&gt;run()</code> method. So just place those assignment statements inside <code>if</code> block.</p> <p>It is better to use <code>trim</code> first in <code>set_rules</code> method. i have used it as first rule. You are already checking <code>callback_usernameTaken</code> in set_rules method so no need to check it again in if statement. <code>usernameTaken</code> is better to be in controller than in model. so moved it there. And finally check out the changes i made to usernameTaken method. I have added <code>$this-&gt;form_validation-&gt;set_message</code> method call and interchanged <code>true</code> and <code>false</code>.</p> <pre><code>function register() { $this-&gt;load-&gt;helper(array('form', 'url')); $this-&gt;load-&gt;library('form_validation'); $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'trim|required|callback_usernameTaken|min_length[5]|max_length[12]'); $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'trim|required|md5'); if ( $this-&gt;form_validation-&gt;run() ){ $username = $this-&gt;input-&gt;post('username'); $password = $this-&gt;input-&gt;post('password'); $this-&gt;membership-&gt;newUser($username, $password); $this-&gt;session-&gt;set_userdata('status', 'OK'); $this-&gt;session-&gt;set_userdata('username', $username); redirect('home'); } else { $this-&gt;session-&gt;set_userdata('status', 'NOT_OK'); $this-&gt;load-&gt;view('shared/header'); $this-&gt;load-&gt;view('account/signuptitle'); $this-&gt;load-&gt;view('account/signupview'); $this-&gt;load-&gt;view('shared/footer'); } } function usernameTaken($username) { $this-&gt;db-&gt;select('*')-&gt;from ('membership')-&gt;where('username', $username); $query = $this-&gt;db-&gt;get(); if ($query-&gt;num_rows &gt; 0) { $this-&gt;form_validation-&gt;set_message('usernameTaken', 'username taken'); return false; } else{ return true; } } </code></pre> <p>CodeIgniter's form-validation class provides both form validation and data prepping features. <a href="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html" rel="nofollow">http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html</a> So there is nothing wrong in using <code>md5</code> in <code>set_rules</code> method. It will run <code>md5</code> method and return the encrypted string.</p> <p>And for better encryption use <code>Mcrypt</code> extension: php.net/manual/en/intro.mcrypt.php or <code>crypt</code> function: php.net/manual/en/function.crypt.php.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload