Skip to content

[Validator] Added CssColor constraint #40168

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

Merged
merged 1 commit into from
Oct 6, 2021

Conversation

welcoMattic
Copy link
Member

@welcoMattic welcoMattic commented Feb 12, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#14965

This PR introduces a new CssColor constraint. It comes with 3 validation modes:

  • Long, which allows all hexadecimal representation of a color, or 9 (#EEEEEEFF) characters
  • Short, which only allows hexadecimal colors on 4 (#EEE), 5 (#FFF00) characters
  • Named colors, which matches the official list of named colors
  • HTML5, which allows hexadecimal colors on 7 (#EEEEEE) characters as well as the HTML5 input type color

I know that such a color validation already exists in Symfony (in the ColorType class), but it's hardcoded in the FormType, and not usable as an assert Annotation. We could decide to remove this hardcoded validation in favor of the new added CssColor constraint. Let me know, if yes, I will make the change.

@norkunas
Copy link
Contributor

Why only hex? Maybe Color constraint with hex, rgb(a),hsl(a) support would be better :)

@welcoMattic
Copy link
Member Author

@norkunas Yes, I thought about it. But I was not sure if we have to support hex,rgb(a),hsl(a) in the same Constraint. I think that we should make separate Constraints to avoid to guess the format. WDYT?

@norkunas
Copy link
Contributor

Also good idea :)

@javiereguiluz
Copy link
Member

javiereguiluz commented Feb 12, 2021

Actually, CSS 4 defines four kinds of hexadecimal colors (https://www.w3.org/TR/css-color-4/#typedef-hex-color):

  • 6-digit: #00ff00
  • 8-digit: #0000ffcc
  • 3-digit: #0f0
  • 4-digit: #0f0c

@xabbuh xabbuh added this to the 5.x milestone Feb 12, 2021
@fancyweb
Copy link
Contributor

Interesting previous discussions on #35626

@OskarStark
Copy link
Contributor

I would prefer HexColor instead of HexaColor

@javiereguiluz
Copy link
Member

Here are some comments in case we want to move this forward:

  • I'd rename HexaColor to CssColor, because this constraint is only about CSS-valid colors
  • CssColor would also match Symfony's primary target: the web (and we use CSS colors even when outside the web; e.g. Symfony's Console true colors are defined with some of the CSS-valid formats)
  • Even if it's called CssColor we don't need to support from the beginning all current (and future) ways of defining CSS colors. This is the same as CardScheme constraint: it only supported a few of the most popular cards at first and, in the following years, we added more cards. Even if it's called CardScheme, it doesn't support all the existing card schemes yet ... but that's fine; it's still very useful and widely used (so, we can add a CssColor without supporting ALL color formats at first, specially the less used ones)

@fabpot
Copy link
Member

fabpot commented Jul 4, 2021

@welcoMattic How can we move forward?

@welcoMattic
Copy link
Member Author

@fabpot I intend to apply Javier's comments, and make a first version of this constraint that we can improve later following user's needs.

@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch 3 times, most recently from cf0bba7 to cead49a Compare July 4, 2021 17:08
@welcoMattic
Copy link
Member Author

welcoMattic commented Jul 4, 2021

I addressed @javiereguiluz's comments about making a first usable version of CssColor Constraint, with common formats support, which are:

  • 6-digit: #00ff00
  • 8-digit: #0000ffcc
  • 3-digit: #0f0
  • 4-digit: #0f0c
  • basic named colors (black|red|green|yellow|blue|magenta|cyan|white) (if required for 1st version, we could copy paste the official list of named colors)

Let me know if we are agree on this 1st version

@derrabus derrabus changed the title [Validator] Added HexaColor constraint [Validator] Added CssColor constraint Jul 4, 2021
@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch from 0eae445 to 454d531 Compare July 4, 2021 20:45
Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mathieu, you've done a fantastic job here. This constraint is much more complete than I imagined. It's great!

I left some minor comments ... and I also wonder if modes is the best word to describe the different types of CSS colors allowed by the constraint. I can't suggest a better alternative, but modes doesn't feel 100% correct to me.

Thanks!

@welcoMattic
Copy link
Member Author

welcoMattic commented Sep 29, 2021

@javiereguiluz thanks! About $modes, what do you think about renaming it to $formats?
I've addressed all other comments, thanks for the review

status: needs review

@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch 4 times, most recently from 347b2a1 to 781598a Compare September 29, 2021 21:12
@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch 2 times, most recently from a741651 to 23709e7 Compare September 29, 2021 21:19
@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch from 23709e7 to 8925155 Compare September 30, 2021 07:16
@welcoMattic welcoMattic force-pushed the hexadecimal-constraint branch from 8925155 to b36371c Compare September 30, 2021 09:05
@welcoMattic
Copy link
Member Author

Fabbot is wrong about the italian typo correction, it's possibile and not possible.

@welcoMattic
Copy link
Member Author

@javiereguiluz @derrabus I don't know exactly when the feature freeze will happen, but I think this PR is ready to be merged in 5.4.

@derrabus
Copy link
Member

derrabus commented Oct 6, 2021

I agree, this PR is ready. But I need a second approval to merge a feature PR.

@fabpot
Copy link
Member

fabpot commented Oct 6, 2021

Thank you @welcoMattic.

@fabpot fabpot merged commit 428434c into symfony:5.4 Oct 6, 2021
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Oct 8, 2021
This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Added CssColor constraint

This PR introduces the new `CssColor` constraint, contributed in symfony/symfony#40168

Commits
-------

fc1539c Added CssColor constraint
@welcoMattic welcoMattic deleted the hexadecimal-constraint branch October 8, 2021 09:50
*/
public function __construct($formats, string $message = null, array $groups = null, $payload = null, array $options = null)
{
$validationModesAsString = array_reduce(self::$validationModes, function ($carry, $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'm reading this PR and wondering if it wouldn't be a good idea to type things as much as we can when doing a chance?

Since Symfony 6 has better typing support, wouldn't it be a good idea to require adding typing when submitting a PR ?

Thanks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be great, but in this case I made a little mistake, this array_reduce is overengineered, it has to be a implode call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it twice now, and indeed, it could be replace with an implode :)

This was referenced Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.