Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 3 form validation callback not working

I have a custom callback in my codeigniter validation that is not getting called, I just can't figure out where the error is.

This is my controller:

$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('pedido', 'numero de pedido', 'trim|required|integer');
$this->form_validation->set_rules('cliente', 'nombre del cliente', 'trim|required|alpha_numeric_spaces');
$this->form_validation->set_rules('motivo', 'motivo de la devolucion', 'trim|required|alpha_numeric_spaces');
$this->form_validation->set_rules('imagen', 'imagen', 'callback_handle_upload');

if ($this->form_validation->run() == FALSE) {
    $error_array = array(
        'pedido' => form_error('pedido'),
        'cliente' => form_error('cliente'),
        'motivo' => form_error('motivo'),
        'imagen' => form_error('imagen')
    );

    $this->session->set_userdata('notify', $error_array);
    redirect('garantias-y-devoluciones');
    die();
}

This is my callback function

public function handle_upload($str)
{
    $this->form_validation->set_message('imagen', 'The {field} has an error.');
    return FALSE;
}

It should trigger the error but its not.

like image 746
AL DI Avatar asked Nov 07 '25 16:11

AL DI


1 Answers

SOLVED.! If anyone gets this same error, this is how i solve it, it is a bug with HMVC "before assigning the current controller as the $CI variable to the form_validation library. This will allow your callback methods to function properly."

Just add public $CI in you MY_Form_validation

then on your controllers constructor:

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

more info on the following page: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

like image 132
AL DI Avatar answered Nov 09 '25 10:11

AL DI