Skip to content

Commit

Permalink
[Form][Validator] Generate accept attribute with file constraint and …
Browse files Browse the repository at this point in the history
…mime types option
  • Loading branch information
Coosos authored and nicolas-grekas committed Jul 24, 2019
1 parent ba988ac commit 5a7b737
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 @@ -5,6 +5,7 @@ CHANGELOG
-----

* 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 5a7b737

Please sign in to comment.