Skip to content

Commit a879ffb

Browse files
committed
Merge branch '2.6' into 2.7
* 2.6: [PropertyAccessor] Added test to allow null value for a array [Yaml] Fixed #10597: Improved Yaml directive parsing [Form] Fixed check of violation constraint #12792 [Form] Set a child type to text if added to the form without a type.
2 parents bf18a60 + 8336ba8 commit a879ffb

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

Extension/Validator/EventListener/ValidationListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function validateForm(FormEvent $event)
6666
foreach ($violations as $violation) {
6767
// Allow the "invalid" constraint to be put onto
6868
// non-synchronized forms
69-
$allowNonSynchronized = $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
69+
// ConstraintViolation::getConstraint() must not expect to provide a constraint as long as Symfony\Component\Validator\ExecutionContext exists (before 3.0)
70+
$allowNonSynchronized = (null === $violation->getConstraint() || $violation->getConstraint() instanceof Form) && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
7071

7172
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
7273
}

Form.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,10 @@ public function add($child, $type = null, array $options = array())
909909
// Never initialize child forms automatically
910910
$options['auto_initialize'] = false;
911911

912+
if (null === $type && null === $this->config->getDataClass()) {
913+
$type = 'text';
914+
}
915+
912916
if (null === $type) {
913917
$child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
914918
} else {

Tests/CompoundFormTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public function testAddUsingIntegerNameAndType()
205205
$this->assertSame(array(0 => $child), $this->form->all());
206206
}
207207

208+
public function testAddWithoutType()
209+
{
210+
$child = $this->getBuilder('foo')->getForm();
211+
212+
$this->factory->expects($this->once())
213+
->method('createNamed')
214+
->with('foo', 'text')
215+
->will($this->returnValue($child));
216+
217+
$this->form->add('foo');
218+
219+
$this->assertTrue($this->form->has('foo'));
220+
$this->assertSame($this->form, $child->getParent());
221+
$this->assertSame(array('foo' => $child), $this->form->all());
222+
}
223+
208224
public function testAddUsingNameButNoType()
209225
{
210226
$this->form = $this->getBuilder('name', null, '\stdClass')

Tests/Extension/Validator/EventListener/ValidationListenerTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ protected function setUp()
6464
$this->params = array('foo' => 'bar');
6565
}
6666

67-
private function getConstraintViolation($code = null)
67+
private function getConstraintViolation($code = null, $constraint = null)
6868
{
69-
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, new Form());
69+
return new ConstraintViolation($this->message, $this->messageTemplate, $this->params, null, 'prop.path', null, null, $code, $constraint);
7070
}
7171

7272
private function getBuilder($name = 'name', $propertyPath = null, $dataClass = null)
@@ -93,7 +93,7 @@ private function getMockForm()
9393
// More specific mapping tests can be found in ViolationMapperTest
9494
public function testMapViolation()
9595
{
96-
$violation = $this->getConstraintViolation();
96+
$violation = $this->getConstraintViolation(null, new Form());
9797
$form = $this->getForm('street');
9898

9999
$this->validator->expects($this->once())
@@ -109,7 +109,28 @@ public function testMapViolation()
109109

110110
public function testMapViolationAllowsNonSyncIfInvalid()
111111
{
112-
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR);
112+
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, new Form());
113+
$form = $this->getForm('street');
114+
115+
$this->validator->expects($this->once())
116+
->method('validate')
117+
->will($this->returnValue(array($violation)));
118+
119+
$this->violationMapper->expects($this->once())
120+
->method('mapViolation')
121+
// pass true now
122+
->with($violation, $form, true);
123+
124+
$this->listener->validateForm(new FormEvent($form, null));
125+
}
126+
127+
public function testMapViolationAllowsNonSyncIfInvalidWithoutConstraintReference()
128+
{
129+
// constraint violations have no reference to the constraint if they are created by
130+
// Symfony\Component\Validator\ExecutionContext
131+
// which is deprecated in favor of
132+
// Symfony\Component\Validator\Context\ExecutionContext
133+
$violation = $this->getConstraintViolation(Form::NOT_SYNCHRONIZED_ERROR, null);
113134
$form = $this->getForm('street');
114135

115136
$this->validator->expects($this->once())

0 commit comments

Comments
 (0)