Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #36302 [Form] Add the html5 option to ColorType to validate t…
…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
- Loading branch information
Showing
7 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/Symfony/Component/Form/Tests/Extension/Core/Type/ColorTypeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Extension\Core\Type; | ||
|
||
use Symfony\Component\Form\Extension\Core\Type\ColorType; | ||
use Symfony\Component\Form\FormError; | ||
|
||
final class ColorTypeTest extends BaseTypeTest | ||
{ | ||
const TESTED_TYPE = ColorType::class; | ||
|
||
/** | ||
* @dataProvider validationShouldPassProvider | ||
*/ | ||
public function testValidationShouldPass(bool $html5, ?string $submittedValue) | ||
{ | ||
$form = $this->factory->create(static::TESTED_TYPE, null, [ | ||
'html5' => $html5, | ||
'trim' => true, | ||
]); | ||
|
||
$form->submit($submittedValue); | ||
|
||
$this->assertEmpty($form->getErrors()); | ||
} | ||
|
||
public function validationShouldPassProvider() | ||
{ | ||
return [ | ||
[false, 'foo'], | ||
[false, null], | ||
[false, ''], | ||
[false, ' '], | ||
[true, '#000000'], | ||
[true, '#abcabc'], | ||
[true, '#BbBbBb'], | ||
[true, '#1Ee54d'], | ||
[true, ' #1Ee54d '], | ||
[true, null], | ||
[true, ''], | ||
[true, ' '], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider validationShouldFailProvider | ||
*/ | ||
public function testValidationShouldFail(string $expectedValueParameterValue, ?string $submittedValue, bool $trim = true) | ||
{ | ||
$form = $this->factory->create(static::TESTED_TYPE, null, [ | ||
'html5' => true, | ||
'trim' => $trim, | ||
]); | ||
|
||
$form->submit($submittedValue); | ||
|
||
$expectedFormError = new FormError('This value is not a valid HTML5 color.', 'This value is not a valid HTML5 color.', [ | ||
'{{ value }}' => $expectedValueParameterValue, | ||
]); | ||
$expectedFormError->setOrigin($form); | ||
|
||
$this->assertEquals([$expectedFormError], iterator_to_array($form->getErrors())); | ||
} | ||
|
||
public function validationShouldFailProvider() | ||
{ | ||
return [ | ||
['foo', 'foo'], | ||
['000000', '000000'], | ||
['#abcabg', '#abcabg'], | ||
['#12345', '#12345'], | ||
[' #ffffff ', ' #ffffff ', false], | ||
]; | ||
} | ||
|
||
public function testSubmitNull($expected = null, $norm = null, $view = null) | ||
{ | ||
parent::testSubmitNull($expected, $norm, ''); | ||
} | ||
} |