yardster
yardster

Reputation: 37

CodeIgniter "form_validation.php" not working as expected?

I have got stummped on a problem where I can not see why codeIgniter is ignoring my rules setup in the "form_validation.php" in which i am using the following code.

$config = array(
'racedetails' => array(
    array(
        'field' => 'race_memberno',
        'label' => 'MembershipNumber',
        'rules' => 'required' 
    ),
    array(
        'field' => 'race_penalties',
        'label' => 'Penalties',
        'rules' => 'required' 
    )
);

I am then calling the Validation set on my controller using:

$this->form_validation->run('racedetails');

However it always states false when ran, The form runs normal and returns no errors , Is there somthing else I may have missed ?

The Above Validation run function runs within the following (Requested by Dale)

public function index($id){
    $this->load->library('form_validation');
    if($this->form_validation->run('racedetails')){$validated++;} 

    if($validated != 1){
        $this->process_template_build('entries/entry',$data);
    }else {
      echo "Validation Passed";
    }
}

Upvotes: 1

Views: 1368

Answers (2)

Michael
Michael

Reputation: 3238

I had the same problem and the reason was, that I´m using an own Form_validation class to stlye the error output globally.

There I forgot to pass the rules to the parent constructor. So this is how it works with an own MY_Form_Validation class:

class MY_Form_validation extends CI_Form_validation {

public function __construct($rules = array())
{
    parent::__construct($rules);

    $this->_error_prefix = '<div class="alert alert-danger" role="alert"><span class="title"><i class="icon-remove-sign"></i> ERROR</span>';
    $this->_error_suffix = '</div>';

}

}

Upvotes: 1

Robert
Robert

Reputation: 1907

Didn't you forget to actually use the $config file before running the validation?

$this->form_validation->set_rules($config);

Upvotes: 0

Related Questions