4

If the first field called sendEmail has value ="Yes" the rest of the fields are to be validated... I have this set_rules defined in the form_validation.php file in the config folder..

$config = array(
array(
      'field'   => 'sendEmail',
      'label'   => 'Send Email',
      'rules'   => 'required'
     ),
     array(
       'field'   => 'email',
       'label'   => 'Email',
       'rules'   => 'required'
     ),
     array(
       'field'   => 'first_name',
       'label'   => 'First Name',
       'rules'   => 'required'
     ),   
    array(
      'field'   => 'last_name',
      'label'   => 'Last Name',
      'rules'   => 'required'
    )
);

but email, first_name and last_name fields are to be validated only if sendEmail has value="Yes".
Not sure how to do this, can someone please help me with this? thanks

2 Answers 2

6

Do the conditions like this in your controller:

if ($this->input->post('sendEmail') == 'Yes'){

  $this->form_validation->set_rules('email', 'Email', 'required');
  ... [etc]
}
Sign up to request clarification or add additional context in comments.

Comments

1

The validation only runs when you call $this->form_validation->run(), so just write an if statement before that. Something like this:

if ($this->input->post("sendEmail") == 'Yes') {
    $this->form_validation->set_rules($config);
    if ($this->form_validation->run() == FALSE)
    {
        // failed validated
    }
    else
    {
        // passed validation
    }
} else {
    // never attempted validation
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.