Showing error in CodeIgniter Form Validation Class only if certain condition meet
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 "example@example.com" with confirmation code "12345". If he submit "example@example.com" with code "22323" 开发者_运维问答then the system will show error message:
- invalid confirmation code
No problem with this case.
But, if somebody submit "this.is.not.exist@example.com" 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.
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
.
精彩评论