Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at the following documentation: <a href="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks" rel="nofollow">http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks</a></p> <p>As you can see int the documentation, the custom validation function actually belongs in the controller, and not in the config file. By moving the validation function to the controller, the callback function should start getting called.</p> <p>Another fun fact, people can access this unique_email function through a url (ie. <a href="http://yoursite.com/index.php/user/unique_email" rel="nofollow">http://yoursite.com/index.php/user/unique_email</a>). To avoid this, we can write the function as a private function by simply placing an underscore at the beginning of the function, like so:</p> <pre><code>function _unique_email($email) { ... } </code></pre> <p>You can then call the function in your validation by using the new function name in your config (notice the extra underscore in the callback:</p> <pre><code>array( 'field' =&gt; 'email', 'label' =&gt; 'Email', 'rules' =&gt; 'required|valid_email|callback__unique_email' ) </code></pre> <p>In the end, your controller should look similar to the following:</p> <pre><code>class User extends CI_Controller { public function register() { $this-&gt;load-&gt;helper('form'); $data['message'] = ''; if($this-&gt;input-&gt;post('submit')) { $this-&gt;load-&gt;library('form_validation'); if($this-&gt;form_validation-&gt;run() == FALSE) { $data['message'] = 'User could not be saved.'; } else { $user_data['firstname'] = $this-&gt;input-&gt;post('firstname'); $user_data['lastname'] = $this-&gt;input-&gt;post('lastname'); $user_data['email'] = $this-&gt;input-&gt;post('email'); $user_data['password'] = md5($this-&gt;input-&gt;post('password')); if($this-&gt;user_model-&gt;insert($user_data)) { if($this-&gt;user_model-&gt;login($user_data)) { $this-&gt;session-&gt;set_flashdata('message', 'User saved successfully.'); redirect('/user', 'refresh'); } } } } $this-&gt;load-&gt;view('user/register', $data); } function _unique_email($email) { if($email == 'm@gmail.com') { $this-&gt;form_validation-&gt;set_message('unique_email', 'Hello World !'); return false; } } } </code></pre> <p>Your config would look similar to the following:</p> <pre><code>$config = array( array( 'field' =&gt; 'firstname', 'label' =&gt; 'First Name', 'rules' =&gt; 'required' ), array( 'field' =&gt; 'lastname', 'label' =&gt; 'Last Name', 'rules' =&gt; 'required' ), array( 'field' =&gt; 'email', 'label' =&gt; 'Email', 'rules' =&gt; 'required|valid_email|callback__unique_email' ), array( 'field' =&gt; 'password', 'label' =&gt; 'Password', 'rules' =&gt; 'required|matches[confirm_password]' ), array( 'field' =&gt; 'confirm_password', 'label' =&gt; 'Confirm Password', 'rules' =&gt; 'required' ) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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