[Form][Validator] Generate accept attribute with file constraint and mime types option
This commit is contained in:
parent
ba988acaec
commit
5a7b737ea3
@ -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
|
||||
-----
|
||||
|
@ -122,7 +122,12 @@ class ValidatorTypeGuesser implements FormTypeGuesserInterface
|
||||
|
||||
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);
|
||||
|
@ -16,6 +16,7 @@ use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
|
||||
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;
|
||||
@ -111,6 +112,43 @@ class ValidatorTypeGuesserTest extends TestCase
|
||||
$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 [
|
||||
|
Reference in New Issue
Block a user