Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 4.4:
  Fix abstract method name in PHP doc block
  Various cleanups
  [HttpClient] fix issues in tests
  Fixes sprintf(): Too few arguments in form transformer
  [Console] Fix QuestionHelper::disableStty()
  [Validator] Use Mime component to determine mime type for file validator
  validate subforms in all validation groups
  Update Hungarian translations
  Add meaningful message when Process is not installed (ProcessHelper)
  [PropertyAccess] Fix TypeError parsing again.
  [TwigBridge] fix fallback html-to-txt body converter
  [Form] add missing Czech validators translation
  [Validator] add missing Czech translations
  never directly validate Existence (Required/Optional) constraints
  • Loading branch information
nicolas-grekas committed May 30, 2020
2 parents 988fbc1 + 2fae337 commit 8bc3321
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
13 changes: 10 additions & 3 deletions Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

use Symfony\Component\HttpFoundation\File\File as FileObject;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

Expand Down Expand Up @@ -170,12 +172,17 @@ public function validate($value, Constraint $constraint)
}

if ($constraint->mimeTypes) {
if (!$value instanceof FileObject) {
$value = new FileObject($value);
if ($value instanceof FileObject) {
$mime = $value->getMimeType();
} elseif (class_exists(MimeTypes::class)) {
$mime = MimeTypes::getDefault()->guessMimeType($path);
} elseif (!class_exists(FileObject::class)) {
throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
} else {
$mime = (new FileObject($value))->getMimeType();
}

$mimeTypes = (array) $constraint->mimeTypes;
$mime = $value->getMimeType();

foreach ($mimeTypes as $mimeType) {
if ($mimeType === $mime) {
Expand Down
48 changes: 48 additions & 0 deletions Resources/translations/validators.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,54 @@
<source>This value should be valid JSON.</source>
<target>Tato hodnota musí být validní JSON.</target>
</trans-unit>
<trans-unit id="87">
<source>This collection should contain only unique elements.</source>
<target>Tato kolekce musí obsahovat pouze unikátní prvky.</target>
</trans-unit>
<trans-unit id="88">
<source>This value should be positive.</source>
<target>Tato hodnota musí být kladná.</target>
</trans-unit>
<trans-unit id="89">
<source>This value should be either positive or zero.</source>
<target>Tato hodnota musí být buď kladná nebo nula.</target>
</trans-unit>
<trans-unit id="90">
<source>This value should be negative.</source>
<target>Tato hodnota musí být záporná.</target>
</trans-unit>
<trans-unit id="91">
<source>This value should be either negative or zero.</source>
<target>Tato hodnota musí být buď záporná nebo nula.</target>
</trans-unit>
<trans-unit id="92">
<source>This value is not a valid timezone.</source>
<target>Tato časová zóna neexistuje.</target>
</trans-unit>
<trans-unit id="93">
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
<target>Zadané heslo bylo součástí úniku dat, takže ho není možné použít. Použijte prosím jiné heslo.</target>
</trans-unit>
<trans-unit id="94">
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Hodnota musí být mezi {{ min }} a {{ max }}.</target>
</trans-unit>
<trans-unit id="95">
<source>This value is not a valid hostname.</source>
<target>Tato hodnota není platný hostname.</target>
</trans-unit>
<trans-unit id="96">
<source>The number of elements in this collection should be a multiple of {{ compared_value }}.</source>
<target>Počet prvků v této kolekci musí být násobek {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="97">
<source>This value should satisfy at least one of the following constraints:</source>
<target>Tato hodnota musí splňovat alespoň jedno z následujících omezení:</target>
</trans-unit>
<trans-unit id="98">
<source>Each element of this collection should satisfy its own set of constraints.</source>
<target>Každý prvek v této kolekci musí splňovat svá vlastní omezení.</target>
</trans-unit>
</body>
</file>
</xliff>
16 changes: 16 additions & 0 deletions Tests/Validator/RecursiveValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -159,4 +161,18 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
$this->assertInstanceOf(Length::class, $violations->get(2)->getConstraint());
}

public function testRequiredConstraintIsIgnored()
{
$violations = $this->validator->validate([], new Required());

$this->assertCount(0, $violations);
}

public function testOptionalConstraintIsIgnored()
{
$violations = $this->validator->validate([], new Optional());

$this->assertCount(0, $violations);
}
}
5 changes: 5 additions & 0 deletions Validator/RecursiveContextualValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\Existence;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
Expand Down Expand Up @@ -718,6 +719,10 @@ private function validateInGroup($value, ?string $cacheKey, MetadataInterface $m
$context->setGroup($group);

foreach ($metadata->findConstraints($group) as $constraint) {
if ($constraint instanceof Existence) {
continue;
}

// Prevent duplicate validation of constraints, in the case
// that constraints belong to multiple validated groups
if (null !== $cacheKey) {
Expand Down

0 comments on commit 8bc3321

Please sign in to comment.