8000 feature #36302 [Form] Add the html5 option to ColorType to validate t… · symfony/symfony@341ea45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 341ea45

Browse files
committed
feature #36302 [Form] Add the html5 option to ColorType to validate the 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
2 parents b494beb + 454b6ff commit 341ea45

File tree

7 files changed

+162
-1
lines changed

7 files changed

+162
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
<tag name="form.type" />
7575
<argument type="service" id="translator" on-invalid="ignore" />
7676
</service>
77+
<service id="form.type.color" class="Symfony\Component\Form\Extension\Core\Type\ColorType">
78+
<tag name="form.type" />
79+
<argument type="service" id="translator" on-invalid="ignore" />
80+
</service>
7781

7882
<service id="form.type_extension.form.transformation_failure_handling" class="Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension">
7983
<tag name="form.type_extension" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CHANGELOG
1919
is deprecated. The method will be added to the interface in 6.0.
2020
* Added a `rounding_mode` option for the PercentType and correctly round the value when submitted
2121
* Deprecated `Symfony\Component\Form\Extension\Validator\Util\ServerParams` in favor of its parent class `Symfony\Component\Form\Util\ServerParams`
22+
* Added the `html5` option to the `ColorType` to validate the input
2223

2324
5.0.0
2425
-----

src/Symfony/Component/Form/Extension/Core/CoreExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected function loadTypes()
7575
new Type\ResetType(),
7676
new Type\CurrencyType(),
7777
new Type\TelType(),
78-
new Type\ColorType(),
78+
EDBE new Type\ColorType($this->translator),
7979
new Type\WeekType(),
8080
];
8181
}

src/Symfony/Component/Form/Extension/Core/Type/ColorType.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,68 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\Form\FormBuilderInterface;
16+
use Symfony\Component\Form\FormError;
17+
use Symfony\Component\Form\FormEvent;
18+
use Symfony\Component\Form\FormEvents;
19+
use Symfony\Component\OptionsResolver\OptionsResolver;
20+
use Symfony\Contracts\Translation\TranslatorInterface;
1521

1622
class ColorType extends AbstractType
1723
{
24+
/**
25+
* @see https://www.w3.org/TR/html52/sec-forms.html#color-state-typecolor
26+
*/
27+
private const HTML5_PATTERN = '/^#[0-9a-f]{6}$/i';
28+
29+
private $translator;
30+
31+
public function __construct(TranslatorInterface $translator = null)
32+
{
33+
$this->translator = $translator;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function buildForm(FormBuilderInterface $builder, array $options)
40+
{
41+
if (!$options['html5']) {
42+
return;
43+
}
44+
45+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void {
46+
$value = $event->getData();
47+
if (null === $value || '' === $value) {
48+
return;
49+
}
50+
51+
if (\is_string($value) && preg_match(self::HTML5_PATTERN, $value)) {
52+
return;
53+
}
54+
55+
$messageTemplate = 'This value is not a valid HTML5 color.';
56+
$messageParameters = [
57+
'{{ value }}' => is_scalar($value) ? (string) $value : \gettype($value),
58+
];
59+
$message = $this->translator ? $this->translator->trans($messageTemplate, $messageParameters, 'validators') : $messageTemplate;
60+
61+
$event->getForm()->addError(new FormError($message, $messageTemplate, $messageParameters));
62+
});
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function configureOptions(OptionsResolver $resolver)
69+
{
70+
$resolver->setDefaults([
71+
'html5' => false,
72+
]);
73+
74+
$resolver->setAllowedTypes('html5', 'bool');
75+
}
76+
1877
/**
1978
* {@inheritdoc}
2079
*/

src/Symfony/Component/Form/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<source>The CSRF token is invalid. Please try to resubmit the form.</source>
1515
<target>The CSRF token is invalid. Please try to resubmit the form.</target>
1616
</trans-unit>
17+
<trans-unit id="99">
18+
<source>This value is not a valid HTML5 color.</source>
19+
<target>This value is not a valid HTML5 color.</target>
20+
</trans-unit>
1721
</body>
1822
</file>
1923
</xliff>

src/Symfony/Component/Form/Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<source>The CSRF token is invalid. Please try to resubmit the form.</source>
1515
<target>Le jeton CSRF est invalide. Veuillez renvoyer le formulaire.</target>
1616
</trans-unit>
17+
<trans-unit id="99">
18+
<source>This value is not a valid HTML5 color.</source>
19+
<target>Cette valeur n'est pas une couleur HTML5 valide.</target>
20+
</trans-unit>
1721
</body>
1822
</file>
1923
</xliff>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+< 10000 /span>
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\Extension\Core\Type\ColorType;
15+
use Symfony\Component\Form\FormError;
16+
17+
final class ColorTypeTest extends BaseTypeTest
18+
{
19+
const TESTED_TYPE = ColorType::class;
20+
21+
/**
22+
* @dataProvider validationShouldPassProvider
23+
*/
24+
public function testValidationShouldPass(bool $html5, ?string $submittedValue)
25+
{
26+
$form = $this->factory->create(static::TESTED_TYPE, null, [
27+
'html5' => $html5,
28+
'trim' => true,
29+
]);
30+
31+
$form->submit($submittedValue);
32+
33+
$this->assertEmpty($form->getErrors());
34+
}
35+
36+
public function validationShouldPassProvider()
37+
{
38+
return [
39+
[false, 'foo'],
40+
[false, null],
41+
[false, ''],
42+
[false, ' '],
43+
[true, '#000000'],
44+
[true, '#abcabc'],
45+
[true, '#BbBbBb'],
46+
[true, '#1Ee54d'],
47+
[true, ' #1Ee54d '],
48+
[true, null],
49+
[true, ''],
50+
[true, ' '],
51+
];
52+
}
53+
54+
/**
55+
* @dataProvider validationShouldFailProvider
56+
*/
57+
public function testValidationShouldFail(string $expectedValueParameterValue, ?string $submittedValue, bool $trim = true)
58+
{
59+
$form = $this->factory->create(static::TESTED_TYPE, null, [
60+
'html5' => true,
61+
'trim' => $trim,
62+
]);
63+
64+
$form->submit($submittedValue);
65+
66+
$expectedFormError = new FormError('This value is not a valid HTML5 color.', 'This value is not a valid HTML5 color.', [
67+
'{{ value }}' => $expectedValueParameterValue,
68+
]);
69+
$expectedFormError->setOrigin($form);
70+
71+
$this->assertEquals([$expectedFormError], iterator_to_array($form->getErrors()));
72+
}
73+
74+
public function validationShouldFailProvider()
75+
{
76+
return [
77+
['foo', 'foo'],
78+
['000000', '000000'],
79+
['#abcabg', '#abcabg'],
80+
['#12345', '#12345'],
81+
[' #ffffff ', ' #ffffff ', false],
82+
];
83+
}
84+
85+
public function testSubmitNull($expected = null, $norm = null, $view = null)
86+
{
87+
parent::testSubmitNull($expected, $norm, '');
88+
}
89+
}

0 commit comments

Comments
 (0)
0