CodeIgniter Forums
Custom validation method - Issue with error message. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Custom validation method - Issue with error message. (/showthread.php?tid=65335)



Custom validation method - Issue with error message. - happyape - 05-31-2016

Code:
class Form extends CI_Controller {

       public function index()
       {
               $this->load->helper(array('form', 'url'));

               $this->load->library('form_validation');

               $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
               $this->form_validation->set_rules('password', 'Password', 'required');
               $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
               $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

               if ($this->form_validation->run() == FALSE)
               {
                       $this->load->view('myform');
               }
               else
               {
                       $this->load->view('formsuccess');
               }
       }

       public function username_check($str)
       {
               if ($str == 'test')
               {
                       $this->form_validation->set_message('username_check', 'The {field} field can not be the word "test"');
                       return FALSE;
               }
               else
               {
                       return TRUE;
               }
       }

}

The above validation fails with this error message
Code:
"Unable to access an error message corresponding to your field name"

I tried to search on SO and this one seem to be the exact issue I am having - http://stackoverflow.com/questions/32665715/codeigniter-unable-to-access-an-error-message-corresponding-to-your-field-name

The OP was able to resolve this issue (see his own answer)

Code:
1. Create MY_Form_validation.php file in libraries folder and paste following code in it.

if (!defined('BASEPATH')) exit('No direct script access allowed');
   class MY_Form_validation extends CI_Form_validation
   {

   function run($module = '', $group = '')
   {
      (is_object($module)) AND $this->CI = &$module;
       return parent::run($group);
   }
}

   And change if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) thats all folks..


But still didn't resolve the issue for me.
What do I do?


RE: Custom validation method - Issue with error message. - Avenirer - 05-31-2016

I guess you must load the form validation library inside that callback method (username_check...) too. Smile You are calling form_validation but you didn't load the library.


RE: Custom validation method - Issue with error message. - happyape - 05-31-2016

Hello - Actually I have loaded the form validation library in the constructor. Smile The above code was just copied & pasted from the CI documentation as my code was very identical except as mentioned below. http://www.codeigniter.com/userguide3/libraries/form_validation.html?highlight=validation#callbacks-your-own-validation-methods

This rule in the example above works fine so I was wrong to say it doesn't work. I had used an additional rule "email" which was a typo as it should have been "valid_email" and that was causing this error for me.

This is what I had in my rules list.

Code:
$this->form_validation->set_rules('username', 'Username', 'email|callback_username_check');

So basically, everything works well. My typo created the problem for me. Please close my thread. Thank you.