Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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