Welcome Guest, Not a member yet? Register   Sign In
Complex Form Validation? With or Without Rules?
#1

[eluser]Unknown[/eluser]
What is the preferred way to deal with complex form validation?

I have data that is selected from dropdowns that has to be checked to verify that the values submitted exist in the database or not. Do most people just create custom callback functions to check that these values exist, or do they keep form validation for simple rules and then if those simple rules pass then they check the more complicated stuff?

For example:

Code:
$this->form_validation->set_rules('ad_type', 'Ad Type', 'required|is_natural');
$this->form_validation->set_rules('ad_type_sub', 'Clicks', 'required|is_natural');

if ($this->form_validation->run() == FALSE) {
     // print errors
} else {
     $type_id = $this->input->post('ad_type');
     $sub_type_id = $this->input->post('ad_type_sub');
     if ($this->Ads_model->is_valid_ad_type($type_id, $sub_type_id)) {
          // types are valid, proceed with more complex field checks
     } else {
          // print more errors back to the screen
     }
}

OR:

Code:
function check_ad_types(type_id, sub_type_id) {
     //validate
     if (valid) {
          return TRUE;
     } else {
          // set error message
          return FALSE;
     }
}

function form_submit() {
     $this->form_validation->set_rules('ad_type', 'Ad Type', 'required|is_natural');
     $this->form_validation->set_rules('ad_type_sub', 'Clicks', 'required|is_natural|callback_check_ad_types');

     if ($this->form_validation->run() == FALSE) {
          // print errors
     } else {
          //success
     }
}

Certainly the second way is cleaner, does anyone have a preference?
#2

[eluser]iamzozo[/eluser]
I would also go on with the second one Smile It's cleaner, and i think thats why codeigniter has callbacks in validation, to extend with your own.




Theme © iAndrew 2016 - Forum software by © MyBB