I found a Codeigniter way to add the functionality I want. By extending the system/libraries/Form_validation.php.
First create file: application/libraries/MY_Form_validation.php
Then extend the CI_Form_validation class:
<?php
/**
* Created by PhpStorm.
* User: william
* Date: 02/02/2016
* Time: 22:56
*/
class MY_Form_validation extends CI_Form_validation{
/**
* set error message
*
* sets the error message associated with a particular field
*
* @param string $field Field name
* @param string $error Error message
*/
public function setError($field, $error){
$this->_field_data[$field]['error'] = $error;
}
}
Now you can use the form_validation->setError('fieldname','error') to manualy set error messages:
class Test extends CI_Controller{
function index(){
$this->load->library('form_validation');
if (/* your validation outside of the form_validation */) {
$this->form_validation->setError('username', 'Invalid login credentials');
}
$this->load->view('test');
}
}
Note! Use this when you want to do some custom validation without using a callback