⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Problem with callback in validation rule



xheradon

xheradon
  • profile picture
  • Member

Posted 01 March 2017 - 09:57 AM

Hi, im trying to check if email is already in use when editing. If email is not changed from edit form all OK, but if email is changed have to check if new email is not already in use. Im trying to set a callback on email validation rule:

$crud->set_rules('email', 'email', 'required|valid_email|callback_email_check');

Then I've (I have to modify, got from another post)

public function email_check($email) {
        $primary_key = $this->uri->segment(4);     
        dump($email);
        dump($primary_key);
        $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
        $rows = $this->cModel->getAllFor('retailer_acct', 'email', $email);
        if(!empty($primary_key) && is_numeric($primary_key))
        {
            if(count($rows)>0) {
                if($rows[0]['id'] != $primary_key) {
                    $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
                    return false;
                }
            }
            return true;
        } else {
            if(count($rows)>0) {
                $this->form_validation->set_message('email_check', 'The email - ' . $email . ' already is assigned to some other user');
                return false;
            } else {
                return true;
            }
        }
    }

But on firebug I've no dumps and returning always: "FIXME('form_validation_email_check')" and "{"success":false,"error_message":"<p>FIXME('form_validation_email_check')<\/p>\n","error_fields":{"email":"FIXME('form_validation_email_check')"}} JSON.

 

Any help? I think is not calling callback function on validation email... Thanks!

 

EDIT: email field is in another table (i don't know if this has any relation with the problem)


xheradon

xheradon
  • profile picture
  • Member

Posted 02 March 2017 - 08:41 AM

Solution:

 

If anyone is having the same problem, is because of the use of hmvc. To fix it: 

 

In application/libraries/grocery_crud.php:

 

1. add 

protected $hmvc;

2. change 

public function __construct(){}

for

public function __construct($hmvc = null) { $this->hmvc = $hmvc; }

3. replace

protected function form_validation()

	{

		if ($this->form_validation === null)

		{

			$this->form_validation = new grocery_CRUD_Form_validation();



		    if ($this->hmvc)

				$this->form_validation->CI = $this->hmvc;			

			

			$ci = &get_instance();

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

			$ci->form_validation = $this->form_validation;

		}

		return $this->form_validation;

	}

Then, in your controller:

$crud = new grocery_CRUD($this);

instead of 

$crud = new grocery_CRUD();

No more needed. Then, if you use is_unique in $crud->set_rules you will get an error. To fix it just change isset to is_object in /libraries/Form_validation.php:

public function is_unique($str, $field)
	{
            //change isset to is_object()
		sscanf($field, '%[^.].%[^.]', $table, $field);
		return is_object($this->CI->db)
			? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
			: FALSE;
	}

Neeraj Singh

Neeraj Singh
  • profile picture
  • Member

Posted 16 August 2019 - 11:05 AM

 

Solution:

 

If anyone is having the same problem, is because of the use of hmvc. To fix it: 

 

In application/libraries/grocery_crud.php:

 

1. add 

protected $hmvc;

2. change 

public function __construct(){}

for

public function __construct($hmvc = null) { $this->hmvc = $hmvc; }

3. replace

protected function form_validation()

	{

		if ($this->form_validation === null)

		{

			$this->form_validation = new grocery_CRUD_Form_validation();



		    if ($this->hmvc)

				$this->form_validation->CI = $this->hmvc;			

			

			$ci = &get_instance();

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

			$ci->form_validation = $this->form_validation;

		}

		return $this->form_validation;

	}

Then, in your controller:

$crud = new grocery_CRUD($this);

instead of 

$crud = new grocery_CRUD();

No more needed. Then, if you use is_unique in $crud->set_rules you will get an error. To fix it just change isset to is_object in /libraries/Form_validation.php:

public function is_unique($str, $field)
	{
            //change isset to is_object()
		sscanf($field, '%[^.].%[^.]', $table, $field);
		return is_object($this->CI->db)
			? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
			: FALSE;
	}

 

Hello xheradon,

 

Million thanks to solve the mystery :) Yes, I was facing same issue because we are using CI with HMVC extension. I have checked the entire code again and again but still was not able to understand "Why GroceryCRUD set_rules with 'callback_function'" not working. Until I found this solution. Bravo!! and excellent hack Man :)

 

For those who are still figuring the solution, follow the checklist.

 

 

Are you using CodeIgniter as MVC or HMVC?
 

  1. HMVC -> Check if you have updated the file (./application/libraries/Grocery_crud.php) as suggested above.
    1. Before "__construct" inside " "class Grocery_CRUD extends grocery_CRUD_States" add "protected $hmvc;"

       
    2. Update "__construct" with as below:
      public function __construct($hmvc = null)
          {
              $this->hmvc = $hmvc;
          }
       
    3.  Update "form_validation" with as below:
      protected function form_validation()
          {
              if ($this->form_validation === null) {
                  $this->form_validation = new grocery_CRUD_Form_validation();
                  if ($this->hmvc) $this->form_validation->CI = $this->hmvc;
                  $ci = &get_instance();
                  $ci->load->library('form_validation');
                  $ci->form_validation = $this->form_validation;
              }
              return $this->form_validation;
          }

       

    4. Use " $crud = new Grocery_crud($this);" instead "$crud = new Grocery_crud();" in your Controller.
       

    5. GC set_rules example: $crud->set_rules("level_title", 'Level Title Label', 'trim|required|callback_unique_level_field_check');
       

    6. Callback method example:

      public function unique_level_field_check ($level_title)

          {
              if ( empty($level_title))
                  {
                      $this->form_validation->set_message('unique_level_field_check', "Level Title Label should be unique");
                      return FALSE;
                  }
              return TRUE;
          }

       

  2. MVC -> Follow 1.5 and 1.6 only (above).

 

 

Happy GCing :)