Does an email validator need a required validator?


I used this example and I refer to this line:

'email' => ['required', 'email'],

Does it make sense to have a required validator with an email validator? I suspect an empty field to fail because an empty field is not a valid email.

If it’s useless and the page gets updated, I suggest to add an example like here to explain how to use more than one validator where at least one validator has parameters.

If necessary, I can create an issue on Github for this.

Best Regards,

That’s two different things. We require the field, so it cannot be empty, and we validate it.

Well but can an empty field pass the email validator? I think it would make a difference if it’s an OR condition between the two validators but it’s an AND, right?

Yes. If the field is not required, you don’t have to enter anything and the validator doesn’t validate the non-existing input.

Eow :scream: . . . that does surprise me: That behavior is quite unexpected because of course if I want to validate something, then it has to have input . . . (well, it can be seen from both sides; but I was surprised to be not warned by this easy to make mistake then)

Why do you want to validate something that has no input? Of course the field is validated if it does have input.

So no, the email validator does not require a required validator.

I was surprised because beside the email validation in this context I did use a regular expression with the match validator to validate that an input has a min and max length and I think that regular expression wouldn’t be properly evaluated then, without an required validator, right? Same with no input and a number validation: I would expect no input to be interpreted as 0 or null and thus fail more/less than validations.

A field is only validated with a validator if it has input. To enforce input, you have to require it, because otherwise it can be left empty.

Thank you for the explanation. That’s something I will have to write down/bookmark on a very important paper for sure. I would do this wrong quite often.

It’s nothing Kirby specific but how form validation works.