Welcome Guest, Not a member yet? Register   Sign In
Grouping validation rules: possible?
#1

[eluser]KeyStroke[/eluser]
Hi,

Some times I'm asked to include a field in a form that's a multi-part field like a serial number, phone number, date, or whatever else that isn't entered using one field. So is there a way to group specific rules under one name?

For example, if I had a form with a "day", "month" and "year" fields; if the user enters a wrong day for example, he will get "Incorrect date" instead of "incorrect day". Is this possible?

Appreciate your help
#2

[eluser]darkhouse[/eluser]
You could setup a callback for one field that checks the other fields, like this:

Code:
function form_submit(){
   $rules = array(
      array('field'=>'year', 'label'=>'Year', 'rules'=>'trim|numeric|callback_check_date'),
      array('field'=>'month', 'label'=>'Month', 'rules'=>'trim|numeric'),
      array('field'=>'day', 'label'=>'Day', 'rules'=>'trim|numeric')
   );
   $this->form_validation->set_rules($rules);
   if($this->form_validation->run()){
      //success
   }
}

function check_date($year){
   $month = $this->input->post('month');
   $day = $this->input->post('day');
   if(!checkdate($month, $day, $year)){
      $this->form_validation->set_message('check_date', 'Incorrect date');
      return false;
   }
   return true;
}




Theme © iAndrew 2016 - Forum software by © MyBB