Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit345a632

Browse files
committed
do not overwrite the constraint being evaluated
1 parent5b1948e commit345a632

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

‎src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class FormValidator extends ConstraintValidator
2626
/**
2727
* {@inheritdoc}
2828
*/
29-
publicfunctionvalidate($form,Constraint$constraint)
29+
publicfunctionvalidate($form,Constraint$formConstraint)
3030
{
31-
if (!$constraintinstanceof Form) {
32-
thrownewUnexpectedTypeException($constraint,__NAMESPACE__.'\Form');
31+
if (!$formConstraintinstanceof Form) {
32+
thrownewUnexpectedTypeException($formConstraint,__NAMESPACE__.'\Form');
3333
}
3434

3535
if (!$forminstanceof FormInterface) {
@@ -62,8 +62,8 @@ public function validate($form, Constraint $constraint)
6262
// Otherwise validate a constraint only once for the first
6363
// matching group
6464
foreach ($groupsas$group) {
65-
if (\in_array($group,$constraint->groups)) {
66-
$validator->atPath('data')->validate($form->getData(),$constraint,$group);
65+
if (\in_array($group,$formConstraint->groups)) {
66+
$validator->atPath('data')->validate($form->getData(),$formConstraint,$group);
6767
if (\count($this->context->getViolations()) >0) {
6868
break;
6969
}
@@ -113,7 +113,7 @@ public function validate($form, Constraint $constraint)
113113
? (string)$form->getViewData()
114114
:\gettype($form->getViewData());
115115

116-
$this->context->setConstraint($constraint);
116+
$this->context->setConstraint($formConstraint);
117117
$this->context->buildViolation($config->getOption('invalid_message'))
118118
->setParameters(array_replace(['{{ value }}' =>$clientDataAsString],$config->getOption('invalid_message_parameters')))
119119
->setInvalidValue($form->getViewData())
@@ -125,7 +125,7 @@ public function validate($form, Constraint $constraint)
125125

126126
// Mark the form with an error if it contains extra fields
127127
if (!$config->getOption('allow_extra_fields') &&\count($form->getExtraData()) >0) {
128-
$this->context->setConstraint($constraint);
128+
$this->context->setConstraint($formConstraint);
129129
$this->context->buildViolation($config->getOption('extra_fields_message'))
130130
->setParameter('{{ extra_fields }}','"'.implode('", "',array_keys($form->getExtraData())).'"')
131131
->setInvalidValue($form->getExtraData())

‎src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
useSymfony\Component\Form\CallbackTransformer;
1515
useSymfony\Component\Form\Exception\TransformationFailedException;
16+
useSymfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1617
useSymfony\Component\Form\Extension\Validator\Constraints\Form;
1718
useSymfony\Component\Form\Extension\Validator\Constraints\FormValidator;
1819
useSymfony\Component\Form\FormBuilder;
1920
useSymfony\Component\Form\FormInterface;
2021
useSymfony\Component\Form\SubmitButtonBuilder;
22+
useSymfony\Component\Translation\IdentityTranslator;
2123
useSymfony\Component\Validator\Constraints\GroupSequence;
2224
useSymfony\Component\Validator\Constraints\NotBlank;
2325
useSymfony\Component\Validator\Constraints\NotNull;
2426
useSymfony\Component\Validator\Constraints\Valid;
27+
useSymfony\Component\Validator\Context\ExecutionContext;
2528
useSymfony\Component\Validator\Test\ConstraintValidatorTestCase;
29+
useSymfony\Component\Validator\Validation;
2630

2731
/**
2832
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -649,6 +653,27 @@ public function getValidationGroups(FormInterface $form)
649653
return ['group1','group2'];
650654
}
651655

656+
publicfunctiontestCauseForNotAllowedExtraFieldsIsTheFormConstraint()
657+
{
658+
$form =$this
659+
->getBuilder('form',null, ['constraints' => [newNotBlank(['groups' => ['foo']])]])
660+
->setCompound(true)
661+
->setDataMapper(newPropertyPathMapper())
662+
->getForm();
663+
$form->submit([
664+
'extra_data' =>'foo',
665+
]);
666+
667+
$context =newExecutionContext(Validation::createValidator(),$form,newIdentityTranslator());
668+
$constraint =newForm();
669+
670+
$this->validator->initialize($context);
671+
$this->validator->validate($form,$constraint);
672+
673+
$this->assertCount(1,$context->getViolations());
674+
$this->assertSame($constraint,$context->getViolations()->get(0)->getConstraint());
675+
}
676+
652677
privatefunctiongetMockExecutionContext()
653678
{
654679
$context =$this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContextInterface')->getMock();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp