Search code examples
phpcodeignitercodeigniter-3hmvc

CodeIgniter + hmvc form_validation rule "is_unique" not applied


I can't seem to figure out my problem hope someone can help. I am using codeigniter 3 +HMVC, in my form validation I use the rule is_unique it works perfect except that if I want to use callbacks, I need to extend the CI_form_validation like so:

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

"When using form validation with MX you will need to extend the CI_Form_validation class as shown below,"

<?php
/** application/libraries/MY_Form_validation **/ 
class MY_Form_validation extends CI_Form_validation 
{
    public $CI;
}

"before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly. (This has been discussed on the CI forums also)."

<?php
class Xyz extends MX_Controller 
{
    function __construct()
    {
        parent::__construct();

        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;
    }
}

When I make this modifications, the rule "is_unique" stops working. Does anyone have an idea of what it is? is it a bug?

this is my code:

class Prod_parent extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->CI =& $this;
    }

 function submit() {
        $this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');     
        $this->form_validation->set_rules('grupo', 'nombre del grupo', 'trim|required|is_unique[prod_parent.product]');
        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('error', validation_errors());
            $this->new();
            die();
        } else {
            die('works great');
        }

Thanks for the help guys....I Need a coffee break!


Solution

  • system/libraries/Form_validation.php in the 1127 row

    change isset() to is_object()

    public function is_unique($str, $field)
    {
        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;
    }