Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit0f352fb

Browse files
committed
[Form] Add the html5 option to ColorType to validate the input
1 parent168574d commit0f352fb

File tree

7 files changed

+150
-1
lines changed

7 files changed

+150
-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
<tagname="form.type" />
7575
<argumenttype="service"id="translator"on-invalid="ignore" />
7676
</service>
77+
<serviceid="form.type.color"class="Symfony\Component\Form\Extension\Core\Type\ColorType"public="true">
78+
<tagname="form.type" />
79+
<argumenttype="service"id="translator"on-invalid="ignore" />
80+
</service>
7781

7882
<serviceid="form.type_extension.form.transformation_failure_handling"class="Symfony\Component\Form\Extension\Core\Type\TransformationFailureExtension">
7983
<tagname="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
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Implementing the`FormConfigBuilderInterface` without implementing the`setIsEmptyCallback()` method
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
19+
* Added the`html5` option to the`ColorType` to validate the input
1920

2021
5.0.0
2122
-----

‎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
newType\ResetType(),
7676
newType\CurrencyType(),
7777
newType\TelType(),
78-
newType\ColorType(),
78+
newType\ColorType($this->translator),
7979
newType\WeekType(),
8080
];
8181
}

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,58 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

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

1622
class ColorTypeextends AbstractType
1723
{
24+
private$translator;
25+
26+
publicfunction__construct(TranslatorInterface$translator =null)
27+
{
28+
$this->translator =$translator;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
publicfunctionbuildForm(FormBuilderInterface$builder,array$options)
35+
{
36+
if (!$options['html5']) {
37+
return;
38+
}
39+
40+
$builder->addEventListener(FormEvents::PRE_SUBMIT,function (FormEvent$event):void {
41+
if (\is_string($value =$event->getData()) &&preg_match('/^#[0-9a-f]{6}$/i',$value)) {
42+
return;
43+
}
44+
45+
$messageTemplate ='This value is not a valid HTML5 color.';
46+
$messageParameters = [
47+
'{{ value }}' =>is_scalar($value) ? (string)$value :\gettype($value),// Logically, the value can only be a string or null.
48+
];
49+
$message =$this->translator ?$this->translator->trans($messageTemplate,$messageParameters,'validators') :strtr($messageTemplate,$messageParameters);
50+
51+
$event->getForm()->addError(newFormError($message,$messageTemplate,$messageParameters));
52+
});
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
publicfunctionconfigureOptions(OptionsResolver$resolver)
59+
{
60+
$resolver->setDefaults([
61+
'html5' =>false,
62+
]);
63+
64+
$resolver->setAllowedTypes('html5','bool');
65+
}
66+
1867
/**
1968
* {@inheritdoc}
2069
*/

‎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-unitid="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-unitid="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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
namespaceSymfony\Component\Form\Tests\Extension\Core\Type;
13+
14+
useSymfony\Component\Form\Extension\Core\Type\ColorType;
15+
useSymfony\Component\Form\FormError;
16+
17+
finalclass ColorTypeTestextends BaseTypeTest
18+
{
19+
constTESTED_TYPE = ColorType::class;
20+
21+
/**
22+
* @dataProvider validationShouldPassProvider
23+
*/
24+
publicfunctiontestValidationShouldPass(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+
publicfunctionvalidationShouldPassProvider()
37+
{
38+
return [
39+
[false,'foo'],
40+
[false,null],
41+
[false,''],
42+
[true,'#000000'],
43+
[true,'#abcabc'],
44+
[true,'#BbBbBb'],
45+
[true,'#1Ee54d'],
46+
[true,' #1Ee54d'],
47+
];
48+
}
49+
50+
/**
51+
* @dataProvider validationShouldFailProvider
52+
*/
53+
publicfunctiontestValidationShouldFail(string$expectedValueParameterValue, ?string$submittedValue,bool$trim =true)
54+
{
55+
$form =$this->factory->create(static::TESTED_TYPE,null, [
56+
'html5' =>true,
57+
'trim' =>$trim,
58+
]);
59+
60+
$form->submit($submittedValue);
61+
62+
$expectedFormError =newFormError('This value is not a valid HTML5 color.','This value is not a valid HTML5 color.', [
63+
'{{ value }}' =>$expectedValueParameterValue,
64+
]);
65+
$expectedFormError->setOrigin($form);
66+
67+
$this->assertEquals([$expectedFormError],iterator_to_array($form->getErrors()));
68+
}
69+
70+
publicfunctionvalidationShouldFailProvider()
71+
{
72+
return [
73+
['foo','foo'],
74+
['NULL',null],
75+
['',''],
76+
['000000','000000'],
77+
['#abcabg','#abcabg'],
78+
['#12345','#12345'],
79+
[' #ffffff',' #ffffff',false],
80+
];
81+
}
82+
83+
publicfunctiontestSubmitNull($expected =null,$norm =null,$view =null)
84+
{
85+
parent::testSubmitNull($expected,$norm,'');
86+
}
87+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp