Welcome Guest, Not a member yet? Register   Sign In
Problem with validation
#1

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
Code:
<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.
Code:
'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
Reply
#2

Just delete
Code:
valid_email|
from validation rules or use your own callbacks:
Code:
'email' => ['label' => 'Email Address', 'rules' => 'callback_email_check|max_length [150]']
Your controller:
Code:
    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;
                }
                else
                {
                        return TRUE;
                }
        }
More about callbacks:
https://codeigniter.com/userguide3/libra...on-methods
Reply
#3

Add permit_empty to your rules: http://codeigniter.com/user_guide/librar...able-rules
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#4

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".
Reply




Theme © iAndrew 2016 - Forum software by © MyBB