-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add the HexColor constraint and validator #35626
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
Conversation
7a60263
to
b716b0d
Compare
|
||
$value = (string) $value; | ||
|
||
if (!preg_match('/^#[0-9a-f]{6}$/i', $value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://www.w3.org/TR/html52/infrastructure.html#valid-simple-color for the spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The definition of color is really broad, so I don't think it's fair to only validate a hex color this way (ignoring #FFF
definitions). if this class would be called HexColorValidator
with a HexColor
constraint, it would make a lot more sense to me.
In the current scenarios, I would think "red", "green", "blue" etc would also be valid, or rgb(255, 0, 0)
for example.
Would be nice to have support for rgb/rgba formats also 🙏 |
I agree with @norkunas. The In case it's useful, check this Gist: https://gist.github.com/olmokramer/82ccce673f86db7cda5e The comments at the end show the regexp needed to match all CSS color formats (including the CSS 4 colors) and even all named CSS colors. |
if we want to accept other formats, it must be optional to me and disabled by default. Otherwise, it means every app will have to check the format to be able to interpret the color. Having only one format simplifies things a lot, especially when it's the only possible format for the input type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supporting other formats would mean this validation could not be used properly with the input type=color field.
Not everyone uses form component, so for the apis this could be a diferrent type of input. But I agree that it should be opt in. |
I agree that validators are not just for forms |
Maybe it's better to name it SimpleColor ( or HexColor ) as the definition of W3C |
Maybe you could add an option to allow only valid lowercase simple color, since it's mentioned in the W3C recommendation? I also opt for changing the name to |
From what I understood, uppercase is authorized: Let's rename it to |
I don't think it's a good idea to restrict this validator to only check one specific way of defining colors. This would be for example as if LanguageValidator only validated languages without any variant (e.g. validate Moreover, validating all valid CSS colors is a matter of applying a single regexp and we now which regexp to use ... so making this an all-purpose validator doesn't seem impossibly hard. |
I meant an only-lowercase option, if somebody wanted a more strict validation. Another option, on the other hand, could allow a short version, e.g. |
Supporting CSS colors makes things complicated because of the large variety of formats:
The linked gist does not take into account all the possibilities. Moreover, I guess browsers interpret the value as they wish: are there norms for allowed spaces, extra/missing characters, bad values, etc.? I think that validating properly all the possibilities is not easy. I think that the Could we maybe move this Or can't we rename it to something very clear like |
@fancyweb yes, you are right that this is not trivial, but let's see if we can implement it:
Use this regex or use an
These ones are easy.
Use this regex
I wasn't aware of this one (because is CSS4) ... but it looks very similar to
Use this regex:
I think it's doable and worth it ... but I won't insist anmore. Now, let's make a decision. Thanks! |
Based on the discussion, we should either not merge it if we want a universal validator or merge it with only what the input supports. In that second case, we should make it very clear that this is the only supported use case. |
let's take this suggestion from @linaori! |
One minor comment: if we call it "HexColorValidator" ... should we validate only 6-Digit hex color (which is the only one used by
I guess we need to support all "hex colors" (because the validator is called HexColorValidator). We may add an |
I think it would be best, if this was configurable through an option. I guess there are many cases, when forms are not used, but constraints are applied, e.g. to validate JSON in an API endpoint. |
The more I read about this one, the more I think that this should not belong to core. Of we really want it, let’s stay focus on the initial use case and name it accordingly to avoid confusion and tons of follow up PRs trying to support more formats. |
382bf2a
to
487f73e
Compare
487f73e
to
22b67cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed the constraint to HexColor
+ created an html5
option.
If the html5
option is true (default is true), then the expected hexadecimal color must be 6 characters long. Otherwise, it can be 3, 4, 6 or 8 characters long.
I'm totally missing the point of all the discussion that happened after the PR was submitted: to me, we should be driven by actual use cases. Here @fancyweb has a clear one, driven by experience: validating color input as generated by html5 color fields. We don't need to solve the problem in a more generic way. Opensource dynamics is about listening to users and giving them a platform to solve realworld use cases more efficiently. It is not to over-engineer a solution to fit a supposed generic problem. With this approach in mind, I'm asking for the PR to get rid of any option and stick to what HTML5-driven use cases need. That'd be immediately helpful. Later on if, maybe, when, unless, until someone comes with a use case and is willing to contribute it to Symfony, then we will have some concrete food for thoughts. For now, that's not the case and we'd better move on. IMHO |
We have an actual use case that we support hex and rgb/rgba formats currently with a custom constraint validator. |
@norkunas great, then you might like to contribute the code once we decided what to do here? |
I've used |
Maybe it could be suggested as egulias/email-validator in EmailValidator |
@fabpot As the project leader I will wait for your decision:
|
Let's name this Html5Color? |
* @see https://www.w3.org/TR/html52/sec-forms.html#color-state-typecolor | ||
*/ | ||
public $html5 = true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: add $errorNames
@fabpot What's your decision on this one? 🙏 |
i dont see much value in a For the validator component a full-featured |
That's a good idea but it doesn't make the constraint reusable at all + if someone overrides the Why we just don't put this new |
I think so, also. Anyone else would object before @fancyweb spends time on it? |
Closing here in favor of #36302. |
…he input (fancyweb) This PR was merged into the 5.1-dev branch. Discussion ---------- [Form] Add the html5 option to ColorType to validate the input | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no | License | MIT | Doc PR | TODO Continuation of #35626. I'm resubmitting the initial implementation, this time in the Form component. This `Color` constraint is dedicated to the HTML5 input type="color". Commits ------- 454b6ff [Form] Add the html5 option to ColorType to validate the input
…he input (fancyweb) This PR was merged into the 5.1-dev branch. Discussion ---------- [Form] Add the html5 option to ColorType to validate the input | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no | License | MIT | Doc PR | TODO Continuation of symfony/symfony#35626. I'm resubmitting the initial implementation, this time in the Form component. This `Color` constraint is dedicated to the HTML5 input type="color". Commits ------- 454b6ff48b [Form] Add the html5 option to ColorType to validate the input
…he input (fancyweb) This PR was merged into the 5.1-dev branch. Discussion ---------- [Form] Add the html5 option to ColorType to validate the input | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no | License | MIT | Doc PR | TODO Continuation of symfony/symfony#35626. I'm resubmitting the initial implementation, this time in the Form component. This `Color` constraint is dedicated to the HTML5 input type="color". Commits ------- 454b6ff48b [Form] Add the html5 option to ColorType to validate the input
From my experience, having this constraint in the core would be very useful to use it with the
ColorType
form type.