0
votes

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!

3

3 Answers

4
votes

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;
}
3
votes

In application/libraries/MY_Form_validation just use below code:

<?php
class MY_Form_validation extends CI_Form_validation {

public $CI;

/**
 * Is Unique
 *
 * Check if the input value doesn't already exist
 * in the specified database field.
 *
 * @param   string  $str
 * @param   string  $field
 * @return  bool
 */
public function is_unique($str, $field)
{
    sscanf($field, '%[^.].%[^.]', $table, $field);
    //return isset($this->CI->db)
    return is_object($this->CI->db)
        ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
        : FALSE;
}
}

This will override is_unique method of form_validation class

0
votes

You can refer this code.

if($this->input->post('user_name') != $original_value) {
   $is_unique =  '|is_unique[users.user_name]'
} else {
   $is_unique =  ''
}

$this->form_validation->set_rules('user_name', 'User Name', 'required|trim|xss_clean'.$is_unique);