Skip to content

Commit c54fefa

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 89bd6ed + ac13428 commit c54fefa

File tree

8 files changed

+73
-22
lines changed

8 files changed

+73
-22
lines changed

Extension/Validator/Constraints/FormValidator.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ public function validate($form, Constraint $formConstraint)
6363
/** @var Constraint[] $constraints */
6464
$constraints = $config->getOption('constraints', []);
6565

66+
$hasChildren = $form->count() > 0;
67+
68+
if ($hasChildren && $form->isRoot()) {
69+
$this->resolvedGroups = new \SplObjectStorage();
70+
}
71+
6672
if ($groups instanceof GroupSequence) {
6773
// Validate the data, the form AND nested fields in sequence
6874
$violationsCount = $this->context->getViolations()->count();
6975
$fieldPropertyPath = \is_object($data) ? 'children[%s]' : 'children%s';
70-
$hasChildren = $form->count() > 0;
71-
$this->resolvedGroups = $hasChildren ? new \SplObjectStorage() : null;
7276

7377
foreach ($groups->groups as $group) {
7478
if ($validateDataGraph) {
@@ -86,20 +90,18 @@ public function validate($form, Constraint $formConstraint)
8690
// sequence recursively, thus some fields could fail
8791
// in different steps without breaking early enough
8892
$this->resolvedGroups[$field] = (array) $group;
89-
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $formConstraint);
93+
$fieldFormConstraint = new Form();
94+
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $fieldFormConstraint);
9095
}
9196
}
9297

9398
if ($violationsCount < $this->context->getViolations()->count()) {
9499
break;
95100
}
96101
}
97-
98-
if ($hasChildren) {
99-
// destroy storage at the end of the sequence to avoid memory leaks
100-
$this->resolvedGroups = null;
101-
}
102102
} else {
103+
$fieldPropertyPath = \is_object($data) ? 'children[%s]' : 'children%s';
104+
103105
if ($validateDataGraph) {
104106
$validator->atPath('data')->validate($data, null, $groups);
105107
}
@@ -131,6 +133,19 @@ public function validate($form, Constraint $formConstraint)
131133
foreach ($groupedConstraints as $group => $constraint) {
132134
$validator->atPath('data')->validate($data, $constraint, $group);
133135
}
136+
137+
foreach ($form->all() as $field) {
138+
if ($field->isSubmitted()) {
139+
$this->resolvedGroups[$field] = $groups;
140+
$fieldFormConstraint = new Form();
141+
$validator->atPath(sprintf($fieldPropertyPath, $field->getPropertyPath()))->validate($field, $fieldFormConstraint);
142+
}
143+
}
144+
}
145+
146+
if ($hasChildren && $form->isRoot()) {
147+
// destroy storage to avoid memory leaks
148+
$this->resolvedGroups = new \SplObjectStorage();
134149
}
135150
} elseif (!$form->isSynchronized()) {
136151
$childrenSynchronized = true;

Extension/Validator/ValidatorExtension.php

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

1414
use Symfony\Component\Form\AbstractExtension;
1515
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
16-
use Symfony\Component\Validator\Constraints\Valid;
16+
use Symfony\Component\Validator\Constraints\Traverse;
1717
use Symfony\Component\Validator\Mapping\ClassMetadata;
1818
use Symfony\Component\Validator\Validator\ValidatorInterface;
1919

@@ -37,7 +37,7 @@ public function __construct(ValidatorInterface $validator)
3737

3838
/* @var $metadata ClassMetadata */
3939
$metadata->addConstraint(new Form());
40-
$metadata->addPropertyConstraint('children', new Valid());
40+
$metadata->addConstraint(new Traverse(false));
4141

4242
$this->validator = $validator;
4343
}

Form.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ private function modelToNorm($value)
10551055
$value = $transformer->transform($value);
10561056
}
10571057
} catch (TransformationFailedException $exception) {
1058-
throw new TransformationFailedException(sprintf('Unable to transform data for property path "%s": '.$exception->getMessage(), $this->getPropertyPath()), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
1058+
throw new TransformationFailedException(sprintf('Unable to transform data for property path "%s": ', $this->getPropertyPath()).$exception->getMessage(), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
10591059
}
10601060

10611061
return $value;
@@ -1077,7 +1077,7 @@ private function normToModel($value)
10771077
$value = $transformers[$i]->reverseTransform($value);
10781078
}
10791079
} catch (TransformationFailedException $exception) {
1080-
throw new TransformationFailedException(sprintf('Unable to reverse value for property path "%s": '.$exception->getMessage(), $this->getPropertyPath()), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
1080+
throw new TransformationFailedException(sprintf('Unable to reverse value for property path "%s": ', $this->getPropertyPath()).$exception->getMessage(), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
10811081
}
10821082

10831083
return $value;
@@ -1106,7 +1106,7 @@ private function normToView($value)
11061106
$value = $transformer->transform($value);
11071107
}
11081108
} catch (TransformationFailedException $exception) {
1109-
throw new TransformationFailedException(sprintf('Unable to transform value for property path "%s": '.$exception->getMessage(), $this->getPropertyPath()), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
1109+
throw new TransformationFailedException(sprintf('Unable to transform value for property path "%s": ', $this->getPropertyPath()).$exception->getMessage(), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
11101110
}
11111111

11121112
return $value;
@@ -1130,7 +1130,7 @@ private function viewToNorm($value)
11301130
$value = $transformers[$i]->reverseTransform($value);
11311131
}
11321132
} catch (TransformationFailedException $exception) {
1133-
throw new TransformationFailedException(sprintf('Unable to reverse value for property path "%s": '.$exception->getMessage(), $this->getPropertyPath()), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
1133+
throw new TransformationFailedException(sprintf('Unable to reverse value for property path "%s": ', $this->getPropertyPath()).$exception->getMessage(), $exception->getCode(), $exception, $exception->getInvalidMessage(), $exception->getInvalidMessageParameters());
11341134
}
11351135

11361136
return $value;

Resources/config/validation.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
<class name="Symfony\Component\Form\Form">
88
<constraint name="Symfony\Component\Form\Extension\Validator\Constraints\Form" />
9-
<property name="children">
10-
<constraint name="Valid" />
11-
</property>
9+
<constraint name="Symfony\Component\Validator\Constraints\Traverse">
10+
<option name="traverse">false</option>
11+
</constraint>
1212
</class>
1313
</constraint-mapping>

Resources/translations/validators.cs.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<source>The CSRF token is invalid. Please try to resubmit the form.</source>
1515
<target>CSRF token je neplatný. Zkuste prosím znovu odeslat formulář.</target>
1616
</trans-unit>
17+
<trans-unit id="99">
18+
<source>This value is not a valid HTML5 color.</source>
19+
<target>Tato hodnota není platná HTML5 barva.</target>
20+
</trans-unit>
1721
</body>
1822
</file>
1923
</xliff>

Resources/translations/validators.hu.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<source>The CSRF token is invalid. Please try to resubmit the form.</source>
1515
<target>Érvénytelen CSRF token. Kérem, próbálja újra elküldeni az űrlapot.</target>
1616
</trans-unit>
17+
<trans-unit id="99">
18+
<source>This value is not a valid HTML5 color.</source>
19+
<target>Ez az érték nem egy érvényes HTML5 szín.</target>
20+
</trans-unit>
1721
</body>
1822
</file>
1923
</xliff>

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ public function testViolationIfExtraData()
658658

659659
$this->assertTrue($form->isSubmitted());
660660
$this->assertTrue($form->isSynchronized());
661-
$this->expectNoValidate();
661+
662+
$this->expectValidateValueAt(0, 'children[child]', $form->get('child'), new Form());
662663

663664
$this->validator->validate($form, new Form());
664665

@@ -681,7 +682,8 @@ public function testViolationFormatIfMultipleExtraFields()
681682

682683
$this->assertTrue($form->isSubmitted());
683684
$this->assertTrue($form->isSynchronized());
684-
$this->expectNoValidate();
685+
686+
$this->expectValidateValueAt(0, 'children[child]', $form->get('child'), new Form());
685687

686688
$this->validator->validate($form, new Form());
687689

Tests/Extension/Validator/ValidatorExtensionTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ public function test2Dot5ValidationApi()
5454
$this->assertInstanceOf(FormConstraint::class, $metadata->getConstraints()[0]);
5555

5656
$this->assertSame(CascadingStrategy::NONE, $metadata->cascadingStrategy);
57-
$this->assertSame(TraversalStrategy::IMPLICIT, $metadata->traversalStrategy);
58-
$this->assertSame(CascadingStrategy::CASCADE, $metadata->getPropertyMetadata('children')[0]->cascadingStrategy);
59-
$this->assertSame(TraversalStrategy::IMPLICIT, $metadata->getPropertyMetadata('children')[0]->traversalStrategy);
57+
$this->assertSame(TraversalStrategy::NONE, $metadata->traversalStrategy);
58+
$this->assertCount(0, $metadata->getPropertyMetadata('children'));
6059
}
6160

6261
public function testDataConstraintsInvalidateFormEvenIfFieldIsNotSubmitted()
@@ -142,6 +141,33 @@ public function testFieldsValidateInSequenceWithNestedGroupsArray()
142141
$this->assertInstanceOf(Length::class, $errors[1]->getCause()->getConstraint());
143142
}
144143

144+
public function testConstraintsInDifferentGroupsOnSingleField()
145+
{
146+
$form = $this->createForm(FormType::class, null, [
147+
'validation_groups' => new GroupSequence(['group1', 'group2']),
148+
])
149+
->add('foo', TextType::class, [
150+
'constraints' => [
151+
new NotBlank([
152+
'groups' => ['group1'],
153+
]),
154+
new Length([
155+
'groups' => ['group2'],
156+
'max' => 3,
157+
]),
158+
],
159+
]);
160+
$form->submit([
161+
'foo' => '[email protected]',
162+
]);
163+
164+
$errors = $form->getErrors(true);
165+
166+
$this->assertFalse($form->isValid());
167+
$this->assertCount(1, $errors);
168+
$this->assertInstanceOf(Length::class, $errors[0]->getCause()->getConstraint());
169+
}
170+
145171
private function createForm($type, $data = null, array $options = [])
146172
{
147173
$validator = Validation::createValidatorBuilder()

0 commit comments

Comments
 (0)