views:

813

answers:

1

First, i would to apologize for my bad english.

Suppose i have function in controller like this :

function confirm()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|exist[member.email]');
        $this->form_validation->set_rules('secretKey','Secret Key', 'required|callback_secret_check');

        $this->form_validation->set_message('exist',"Email don't exist");
        $this->form_validation->set_message('secret_check','Invalid confirmation code');

        if ($this->form_validation->run() == FALSE)
          {
            // if error, show form and the error msg
            $this->view->full_render('user_confirm') ;
          } else {
            // if all ok
            echo "success" ;
          }
    }

In short, this function is used to validate new user email using confirmation code. Just like we found in any site that need registration. I made this function in case some user prefer to submit his confirmation code directly using confirmation form instead of using confirmation url (we send both confirmation url and confirmation code to his email)

Just for your information, the rule "exist" is my own rule created by extending the form class to check if the email submitted is really exist. For the "callback_secret_check" rule, i create callback function (not by extending the form validation class like first rule) to check if the confirmation code submitted by the user is a valid one.

Now, suppose i have user email "[email protected]" with confirmation code "12345". If he submit "[email protected]" with code "22323" then the system will show error message:

  • invalid confirmation code

No problem with this case.

But, if somebody submit "[email protected]" with any confirmation code, the system will show error message

  • Email don't exist
  • Invalid confirmation code

what i wanted is only

  • Email don't exist

without showing "invalid confirmation code" because if the email did not even exist, i think we don't need to tell anything about the confirmation code. Any clue?

thanks for your help.

+2  A: 

In that case, don't set the invalid confirmation code to have required. If the field only 2, you can check it in a simple if. Not all case need form_validation.

Donny Kurnia
Woa.. it's work! (the remove "required" trick). Can you explain it to me? I'll accept your answer soon after that. Thx for the tips anyway.
wildanr
ok, it's a workaround, but what if you need to validate like 100 fields. Then all do them with an `if`??
Natrium
@wildanr, `required` make the field must have value, if not, the error raised. Since now the second field no longer `required`, you must handle it by yourself. That's why I suggest that you do this with simple `if`, you only have 2 field to take care on, and it also need special treatment.@Natrium, i write 'in that case'. Also, if there are 100 fields, then you must do it wrong, since as user, I wouldn't want to fill a 100 field form. The biggest number of field in single form I ever make is never exceed 20 field. If you will need 100 field, try to split it into multiple steps.
Donny Kurnia