Skip to content

Commit

Permalink
feature #32587 [Form][Validator] Generate accept attribute with file …
Browse files Browse the repository at this point in the history
…constraint and mime types option (Coosos)

This PR was squashed before being merged into the 4.4 branch (closes #32587).

Discussion
----------

[Form][Validator] Generate accept attribute with file constraint and mime types option

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes
| Fixed tickets | #29327
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Generate the html `accept` attribute based on the file constraint and mime types option.

Is it necessary to add this feature in the documentation ?

Made with @Timherlaud

_Sorry I have recreated this pull request because I missed my rebase_

Commits
-------

5a7b737 [Form][Validator] Generate accept attribute with file constraint and mime types option
  • Loading branch information
nicolas-grekas committed Jul 24, 2019
2 parents 5af1a90 + 5a7b737 commit 71687e0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* preferred choices are repeated in the list of all choices
* deprecated using `int` or `float` as data for the `NumberType` when the `input` option is set to `string`
* The type guesser guesses the HTML accept attribute when a mime type is configured in the File or Image constraint.

4.3.0
-----
Expand Down
Expand Up @@ -122,7 +122,12 @@ public function guessTypeForConstraint(Constraint $constraint)

case 'Symfony\Component\Validator\Constraints\File':
case 'Symfony\Component\Validator\Constraints\Image':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\FileType', [], Guess::HIGH_CONFIDENCE);
$options = [];
if ($constraint->mimeTypes) {
$options = ['attr' => ['accept' => implode(',', (array) $constraint->mimeTypes)]];
}

return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\FileType', $options, Guess::HIGH_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Language':
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\LanguageType', [], Guess::HIGH_CONFIDENCE);
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
Expand Down Expand Up @@ -111,6 +112,43 @@ public function testGuessMaxLengthForConstraintWithMinValue()
$this->assertNull($result);
}

public function testGuessMimeTypesForConstraintWithMimeTypesValue()
{
$mineTypes = ['image/png', 'image/jpeg'];
$constraint = new File(['mimeTypes' => $mineTypes]);
$typeGuess = $this->guesser->guessTypeForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $typeGuess);
$this->assertArrayHasKey('attr', $typeGuess->getOptions());
$this->assertArrayHasKey('accept', $typeGuess->getOptions()['attr']);
$this->assertEquals(implode(',', $mineTypes), $typeGuess->getOptions()['attr']['accept']);
}

public function testGuessMimeTypesForConstraintWithoutMimeTypesValue()
{
$constraint = new File();
$typeGuess = $this->guesser->guessTypeForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $typeGuess);
$this->assertArrayNotHasKey('attr', $typeGuess->getOptions());
}

public function testGuessMimeTypesForConstraintWithMimeTypesStringValue()
{
$constraint = new File(['mimeTypes' => 'image/*']);
$typeGuess = $this->guesser->guessTypeForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $typeGuess);
$this->assertArrayHasKey('attr', $typeGuess->getOptions());
$this->assertArrayHasKey('accept', $typeGuess->getOptions()['attr']);
$this->assertEquals('image/*', $typeGuess->getOptions()['attr']['accept']);
}

public function testGuessMimeTypesForConstraintWithMimeTypesEmptyStringValue()
{
$constraint = new File(['mimeTypes' => '']);
$typeGuess = $this->guesser->guessTypeForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $typeGuess);
$this->assertArrayNotHasKey('attr', $typeGuess->getOptions());
}

public function maxLengthTypeProvider()
{
return [
Expand Down

0 comments on commit 71687e0

Please sign in to comment.