8000 Email assert passes empty string · Issue #10357 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Email assert passes empty string #10357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yuri-karkh opened this issue Sep 17, 2018 · 4 comments
Closed

Email assert passes empty string #10357

yuri-karkh opened this issue Sep 17, 2018 · 4 comments
Labels
hasPR A Pull Request has already been submitted for this issue. Validator

Comments

@yuri-karkh
Copy link
yuri-karkh commented Sep 17, 2018
    /**
     * @var string
     * @ORM\Column(name="email", type="string", length=100, unique=true)
     * @Assert\Email(
	 *     message="Email is not valid",
	 *     mode="html5"
	 * )
     */
    private $email;

passing empty string as an email passed validation. var_dump of the $validator->validate( $object ); is below.

object(Symfony\Component\Validator\ConstraintViolationList)#3636 (1) {
 ["violations":"Symfony\Component\Validator\ConstraintViolationList":private]=>
 array(0) {
 }
}

Also found similar issue here:
#4244

I can confirm that if use together with NotBlank() constraint Email() constraint works as expected. So not very obvious behaviour of assertion.

@javiereguiluz
Copy link
Member

@yuri-karkh thanks for reporting this. I know it may be confusing at first, but I've asked about this in the Symfony Slack chat and the community told me the reason why Symfony does this.

If EmailValidator displays an error when you pass an empty string, the validator is doing two different things: validating the format of email address and making them mandatory (because if you pass null or empty string, you see an error).

So it's better to keep this as is. If you want to only validate email address, without requiring them, use EmailValidator. If you want to validate emails and require them (not allow empty strings or null values) then use EmailValidator and NotBlank together. I'm going to propose some changes in the docs about this. Thanks!

@yuri-karkh
Copy link
Author

thanks for the reply @javiereguiluz. Agree that it will be better to have some notes in docs about this.

@javiereguiluz
Copy link
Member

We're trying to improve this in #10407.

@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Sep 26, 2018
javiereguiluz added a commit that referenced this issue Sep 28, 2018
This PR was squashed before being merged into the 2.8 branch (closes #10407).

Discussion
----------

Added a note about the usage of NotBlank

Fixes #4244 and fixes #10357.

Commits
-------

24ed17e Added a note about the usage of NotBlank
@javiereguiluz
Copy link
Member

Fixed by #10407.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hasPR A Pull Request has already been submitted for this issue. Validator
Projects
None yet
Development

No branches or pull requests

3 participants
0