Skip to content

Commit 8bc3321

Browse files
Merge branch '4.4' into 5.0
* 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
2 parents 988fbc1 + 2fae337 commit 8bc3321

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

Constraints/FileValidator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\Component\HttpFoundation\File\File as FileObject;
1515
use Symfony\Component\HttpFoundation\File\UploadedFile;
16+
use Symfony\Component\Mime\MimeTypes;
1617
use Symfony\Component\Validator\Constraint;
1718
use Symfony\Component\Validator\ConstraintValidator;
19+
use Symfony\Component\Validator\Exception\LogicException;
1820
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1921
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2022

@@ -170,12 +172,17 @@ public function validate($value, Constraint $constraint)
170172
}
171173

172174
if ($constraint->mimeTypes) {
173-
if (!$value instanceof FileObject) {
174-
$value = new FileObject($value);
175+
if ($value instanceof FileObject) {
176+
$mime = $value->getMimeType();
177+
} elseif (class_exists(MimeTypes::class)) {
178+
$mime = MimeTypes::getDefault()->guessMimeType($path);
179+
} elseif (!class_exists(FileObject::class)) {
180+
throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".');
181+
} else {
182+
$mime = (new FileObject($value))->getMimeType();
175183
}
176184

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

180187
foreach ($mimeTypes as $mimeType) {
181188
if ($mimeType === $mime) {

Resources/translations/validators.cs.xlf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,54 @@
334334
<source>This value should be valid JSON.</source>
335335
<target>Tato hodnota musí být validní JSON.</target>
336336
</trans-unit>
337+
<trans-unit id="87">
338+
<source>This collection should contain only unique elements.</source>
339+
<target>Tato kolekce musí obsahovat pouze unikátní prvky.</target>
340+
</trans-unit>
341+
<trans-unit id="88">
342+
<source>This value should be positive.</source>
343+
<target>Tato hodnota musí být kladná.</target>
344+
</trans-unit>
345+
<trans-unit id="89">
346+
<source>This value should be either positive or zero.</source>
347+
<target>Tato hodnota musí být buď kladná nebo nula.</target>
348+
</trans-unit>
349+
<trans-unit id="90">
350+
<source>This value should be negative.</source>
351+
<target>Tato hodnota musí být záporná.</target>
352+
</trans-unit>
353+
<trans-unit id="91">
354+
<source>This value should be either negative or zero.</source>
355+
<target>Tato hodnota musí být buď záporná nebo nula.</target>
356+
</trans-unit>
357+
<trans-unit id="92">
358+
<source>This value is not a valid timezone.</source>
359+
<target>Tato časová zóna neexistuje.</target>
360+
</trans-unit>
361+
<trans-unit id="93">
362+
<source>This password has been leaked in a data breach, it must not be used. Please use another password.</source>
363+
<target>Zadané heslo bylo součástí úniku dat, takže ho není možné použít. Použijte prosím jiné heslo.</target>
364+
</trans-unit>
365+
<trans-unit id="94">
366+
<source>This value should be between {{ min }} and {{ max }}.</source>
367+
<target>Hodnota musí být mezi {{ min }} a {{ max }}.</target>
368+
</trans-unit>
369+
<trans-unit id="95">
370+
<source>This value is not a valid hostname.</source>
371+
<target>Tato hodnota není platný hostname.</target>
372+
</trans-unit>
373+
<trans-unit id="96">
374+
<source>The number of elements in this collection should be a multiple of {{ compared_value }}.</source>
375+
<target>Počet prvků v této kolekci musí být násobek {{ compared_value }}.</target>
376+
</trans-unit>
377+
<trans-unit id="97">
378+
<source>This value should satisfy at least one of the following constraints:</source>
379+
<target>Tato hodnota musí splňovat alespoň jedno z následujících omezení:</target>
380+
</trans-unit>
381+
<trans-unit id="98">
382+
<source>Each element of this collection should satisfy its own set of constraints.</source>
383+
<target>Každý prvek v této kolekci musí splňovat svá vlastní omezení.</target>
384+
</trans-unit>
337385
</body>
338386
</file>
339387
</xliff>

Tests/Validator/RecursiveValidatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Validator\Constraints\Length;
2020
use Symfony\Component\Validator\Constraints\NotBlank;
2121
use Symfony\Component\Validator\Constraints\NotNull;
22+
use Symfony\Component\Validator\Constraints\Optional;
23+
use Symfony\Component\Validator\Constraints\Required;
2224
use Symfony\Component\Validator\ConstraintValidatorFactory;
2325
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2426
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -159,4 +161,18 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
159161
$this->assertInstanceOf(Length::class, $violations->get(1)->getConstraint());
160162
$this->assertInstanceOf(Length::class, $violations->get(2)->getConstraint());
161163
}
164+
165+
public function testRequiredConstraintIsIgnored()
166+
{
167+
$violations = $this->validator->validate([], new Required());
168+
169+
$this->assertCount(0, $violations);
170+
}
171+
172+
public function testOptionalConstraintIsIgnored()
173+
{
174+
$violations = $this->validator->validate([], new Optional());
175+
176+
$this->assertCount(0, $violations);
177+
}
162178
}

Validator/RecursiveContextualValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\Constraints\Composite;
16+
use Symfony\Component\Validator\Constraints\Existence;
1617
use Symfony\Component\Validator\Constraints\GroupSequence;
1718
use Symfony\Component\Validator\Constraints\Valid;
1819
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
@@ -718,6 +719,10 @@ private function validateInGroup($value, ?string $cacheKey, MetadataInterface $m
718719
$context->setGroup($group);
719720

720721
foreach ($metadata->findConstraints($group) as $constraint) {
722+
if ($constraint instanceof Existence) {
723+
continue;
724+
}
725+
721726
// Prevent duplicate validation of constraints, in the case
722727
// that constraints belong to multiple validated groups
723728
if (null !== $cacheKey) {

0 commit comments

Comments
 (0)