Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

callback function is calling before all validation

Call back validation calling first then required validation is calling. I want to call required validation first.

My set_validation function code:

public function set_validation()
{
    $this->form_validation->set_rules('from', 'Ended Date', 'required|callback_compareDates');
}
like image 847
Shahneel Ahmed Avatar asked Apr 02 '26 03:04

Shahneel Ahmed


1 Answers

In order to alter the sequence of the execution you need modify the core library file in /system/libraries/Form_validation.php

From function

protected function _prepare_rules($rules)
{
...
...
return array_merge($callbacks, $new_rules);
}

Change

return array_merge($callbacks, $new_rules);

To

return array_merge($new_rules, $callbacks);

Read here Form_validation.php

OR

Without modifying core, you have to create on more callback function which does function of rule required like below

function required($str)
{

    if(!is_array($str) ? (empty($str) === FALSE): (trim($str) !== ''))
    {
       $this->form_validation->set_message('from', 'Required error message');
       return false
    }

    return true;
}

and in your controller

public function set_validation()
{
    $this->form_validation->set_rules('from', 'Ended Date', 'callback_required|callback_compareDates');
}
like image 195
Akshay Hegde Avatar answered Apr 08 '26 05:04

Akshay Hegde