CodeIgniter Forums
Problem with validation - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: Problem with validation (/showthread.php?tid=80172)

Problem with validation - PeVal - 09-23-2021

Please, give me an advice

If he has an item in the form that can remain empty (the user does not have to fill it in), then I have a problem with validation.

For example, I have an email item that does not need to be filled out
<input type = "text" name = "email" value = "<? php echo set_value ('email');?>" size = "50" />

But I have a validation written on it, which in my opinion should take place only when something is entered in the item.
'email' => ['label' => 'Email Address', 'rules' => 'valid_email|max_length [150]']

If I do not fill in the email and send the form, it reports the error "The Email Address field must contain a valid email address."

Can you please advise me what to do with it?

Thank you

RE: Problem with validation - janeiro - 09-23-2021

Just delete
from validation rules or use your own callbacks:
'email' => ['label' => 'Email Address', 'rules' => 'callback_email_check|max_length [150]']
Your controller:
    public function email_check($str)
                if ($str == 'test')
                        $this->form_validation->set_message('email_check', 'The {field} field can not be the word "test"');
                        return FALSE;
                        return TRUE;
More about callbacks:

RE: Problem with validation - includebeer - 09-23-2021

Add permit_empty to your rules:

RE: Problem with validation - PeVal - 09-23-2021

Thank you. My fault. Only now have I found out that validation works differently than in CI3 and in "Available Rules", it is now "permit_empty".