8000 [DX][Validation] Text/StringValidation · Issue #11875 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX][Validation] Text/StringValidation #11875

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
peterrehm opened this issue Sep 8, 2014 · 15 comments
Closed

[DX][Validation] Text/StringValidation #11875

peterrehm opened this issue Sep 8, 2014 · 15 comments

Comments

@peterrehm
Copy link
Contributor

We do have the form types text and textarea and in Doctrine the field types text and string.

If you use a string/text you usually expect some data but not special characters like a line feed.
This is no problem if you use the form component since you cannot enter a line feed in the input field.
If you however develop an API then you might get such unwanted characters in e.g. the name or
the street.

Should we add a validation constraint Text? It could have some defaults to disallow some special characters and options to customize that behavior?

With this we can support the developer to have a more consistent database without the hassle of adding an Regex validator.

I think at least double spaces and line breaks should be invalidated. I was using for this /(\s\s)|(\n)/ so far.

What do you think - Does this make sense? What should be invalidated?

@stof
Copy link
Member
stof commented Sep 8, 2014

@peterrehm why not using the Regex constraint for your use case ?

@peterrehm
Copy link
Contributor Author

@stof This is what I am using currently. However I think this might be an even more common use case where we could avoid errors the developers are experiencing.

I just wanted to discuss if this makes sense and if we can think about other usecases.

@stof
Copy link
Member
stof commented Sep 9, 2014

Well, a Text constraint forbidding some configurable chars would either be a duplicate of Regex, or a less powerful version of it. I don't really see the need for it.

@peterrehm
Copy link
Contributor Author

Sure it is a duplicate just to ease the use. I mean the double whitespaces are domain specific but usually if you have a simple string there should not be a line break.

It was just an idea to make life easier for developers.

@peterrehm
Copy link
Contributor Author

I think the disadvantage of the Regex constraint is that a lot of developers are not catching what is actually happening. This reduces readability. On the other side an @Assert\Text() constraint might
hide some parts as well.

    /**
     * @var string firstName
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\Regex(pattern="/(\s\s)|(\n)/", match=false)
     */
    private $firstName;

    /**
     * @var string lastName
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\Regex(pattern="/(\s\s)|(\n)/", match=false)
     */
    private $lastName;

What I could consider is wrapping some common patterns the following way:

    /**
     * @var string lastName
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\Text(newLine = false, doubleWhiteSpaces = false)
     */
    private $lastName;

You immediately see what is happening and this would improve the developer experience.
If you think this makes no sense I can close this one. This was again just an idea I wanted
to discuss.

@stof
Copy link
Member
stof commented Sep 12, 2014

Well, the issue is that people will then ask us to add more and more stuff in this constraint., making it unmaintainable.

so I'm -1 for adding it in core (nothing prevents you to add it in your own project though).

@symfony/deciders what do you think about this ?

@Tobion
Copy link
Contributor
Tobion commented Sep 12, 2014

This is related to #4102
@stof stof your argument that this assertion is already covered by regex is not valid because many other assertions like url, isbn etc could also be achieved with regex.

@stof
Copy link
Member
stof commented Sep 12, 2014

@Tobion the URL regex is totally crazy, and it is a well defined need. On the other hand, this suggest providing a constraint with multiple behaviors which can each be defined with very simple regexes.

thus, it could even be confusing. what would @Assert\Text(newLine = true) mean ? Would it allow to have newlines in your text value, or would it require having newlines in it ?

@Tobion
Copy link
Contributor
Tobion commented Sep 12, 2014

We didn't discuss the syntax yet, it could also be @Assert\TextWithoutNewline

@stof
Copy link
Member
stof commented Sep 12, 2014

@Tobion but then, we would end up with tons of such constraints, because everyone would request their own variations. and this would make the builtin constraints a mess

@peterrehm
Copy link
Contributor Author

Well I just wanted to start the discussion.

It could be @Assert\Text() with sensible defaults where you can customize what you need.
And still the entire Syntax would be another issue.

@Tobion
Copy link
Contributor
Tobion commented Sep 12, 2014

I'm just saying we probably need some sort of text validation anyway. See ##4102

@webmozart
Copy link
Contributor

I think no developer would search for an existing constraint that prevents newlines or double whitespaces. At least for me, this is not something that I expect a framework to provide if there's a constraint for validating regexes available.

The same is not true for URLs, IBANs, credit card numbers etc. There the algorithm is so complex that I definitely would try to find an existing validator first before coding it myself.

Consequently, I don't think it makes sense to add this to core, because nobody would use it. If you want to use such specialized constraints in your own application/dev team, just add them yourselves (you just need to override Regex and hardcode the pattern).

@peterrehm
Copy link
Contributor Author

I have it working on my side. Just thought this would make it consistent. In a text field you should never have a new line.

@webmozart
Copy link
Contributor

I suggest you to propose a constraint OneLine or SingleLine in a new issue and gather feedback from other developers, e.g. via Twitter. I'm open to add this if enough people think this addition is worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
0