Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 454b6ff

Browse filesBrowse files
committed
[Form] Add the html5 option to ColorType to validate the input
1 parent 6dc7d8b commit 454b6ff
Copy full SHA for 454b6ff

File tree

7 files changed

+162
-1
lines changed
Filter options

7 files changed

+162
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CHANGELOG
1717
is deprecated. The method will be added to the interface in 6.0.
1818
* Added a `rounding_mode` option for the PercentType and correctly round the value when submitted
1919
* Deprecated `Symfony\Component\Form\Extension\Validator\Util\ServerParams` in favor of its parent class `Symfony\Component\Form\Util\ServerParams`
20+
* Added the `html5` option to the `ColorType` to validate the input
2021

2122
5.0.0
2223
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/CoreExtension.php
+1-1Lines 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+
new Type\ColorType($this->translator),
7979
new Type\WeekType(),
8080
];
8181
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/ColorType.php
+59Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Resources/translations/validators.en.xlf
+4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Resources/translations/validators.fr.xlf
+4Lines 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>
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.