Skip to content

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

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
<span class="value">{{ request.route ?: '(none)' }}</span>
<span class="label">Matched route</span>
</div>

{% if request.route %}
<div class="metric">
<span class="value">{{ traces|length }}</span>
<span class="label">Tested routes before match</span>
</div>
{% endif %}
</div>

{% if request.route %}
Expand Down
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`
* Render accept attribute for `FileType` if mime types is used in assert validator

4.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ 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);
return new TypeGuess(
'Symfony\Component\Form\Extension\Core\Type\FileType',
($constraint->mimeTypes && \is_array($constraint->mimeTypes)) ?
['attr' => ['accept' => implode(', ', $constraint->mimeTypes)]] :
[],
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,38 @@ public function testGuessMaxLengthForConstraintWithMinValue()
$this->assertNull($result);
}

/**
* Check if FileType contains accept attribute.
*/
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']);
}

/**
* Check if file assert not contains mime types.
*/
public function testGuessMimeTypesForConstraintWithoutMimeTypesValue()
{
$constraint = new File();

$typeGuess = $this->guesser->guessTypeForConstraint($constraint);

$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $typeGuess);

if (isset($typeGuess->getOptions()['attr'])) {
$this->assertArrayNotHasKey('accept', $typeGuess->getOptions()['attr']);
}
}

public function maxLengthTypeProvider()
{
return [
Expand Down