Skip to content

Commit ebb9069

Browse files
committed
Merge branch '2.7'
* 2.7: (22 commits) [DependencyInjection] deprecated synchronized services [FrameworkBundle] adds legacy tests for deprecated configuration keys. [TwigBundle] adds legacy tests for deprecated configuration keys. [PropertyAccessor] Added test to allow null value for a array [Yaml] Fixed #10597: Improved Yaml directive parsing [Validator] always use the lazy loading metadata factory [Validator] removed usage of deprecated getMessageParameters() and getMessagePluralization() in unit tests [Validator] fixed deprecation notices for BuildViolation() calls in constraints [Validator] fixed usage of deprecate Validator features [Validator] removed obsolete code removed the Validator BC layer for PHP < 5.3.9 removed code for PHP < 5.3.9 bumped min PHP version to 5.3.9 fixed deprecation summary and missing error_reporting() in tests [Form] Fixed check of violation constraint #12792 [FrameworkBundle] adds deprecation notice on framework.csrf_protection.field_name configuration key. [TwigBundle] adds missing deprecation notice for the twig.form.resources configuration key. [FrameworkBundle] avoid using deprecated classes for reflection [FrameworkBundle] Add a test case for service aliases used with AddExpressionLanguageProviderPass. [FrameworkBundle] fixed #12847 AddExpressionLanguageProviderPass ... Conflicts: .travis.yml composer.json src/Symfony/Bridge/Doctrine/composer.json src/Symfony/Bridge/Monolog/composer.json src/Symfony/Bridge/Propel1/composer.json src/Symfony/Bridge/ProxyManager/composer.json src/Symfony/Bridge/Swiftmailer/composer.json src/Symfony/Bridge/Twig/composer.json src/Symfony/Bundle/DebugBundle/composer.json src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php src/Symfony/Bundle/FrameworkBundle/composer.json src/Symfony/Bundle/SecurityBundle/composer.json src/Symfony/Bundle/TwigBundle/composer.json src/Symfony/Bundle/WebProfilerBundle/composer.json src/Symfony/Component/BrowserKit/composer.json src/Symfony/Component/ClassLoader/composer.json src/Symfony/Component/Config/composer.json src/Symfony/Component/Console/composer.json src/Symfony/Component/CssSelector/composer.json src/Symfony/Component/Debug/composer.json src/Symfony/Component/DependencyInjection/composer.json src/Symfony/Component/DomCrawler/composer.json src/Symfony/Component/EventDispatcher/composer.json src/Symfony/Component/ExpressionLanguage/composer.json src/Symfony/Component/Filesystem/composer.json src/Symfony/Component/Finder/composer.json src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToStringTransformerTest.php src/Symfony/Component/Form/composer.json src/Symfony/Component/HttpFoundation/composer.json src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php src/Symfony/Component/HttpKernel/composer.json src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php src/Symfony/Component/Intl/Tests/DateFormatter/AbstractIntlDateFormatterTest.php src/Symfony/Component/Intl/Tests/DateFormatter/IntlDateFormatterTest.php src/Symfony/Component/Intl/composer.json src/Symfony/Component/Locale/composer.json src/Symfony/Component/OptionsResolver/composer.json src/Symfony/Component/Process/composer.json src/Symfony/Component/PropertyAccess/composer.json src/Symfony/Component/Routing/composer.json src/Symfony/Component/Security/Acl/composer.json src/Symfony/Component/Security/Core/composer.json src/Symfony/Component/Security/Csrf/composer.json src/Symfony/Component/Security/Http/composer.json src/Symfony/Component/Security/composer.json src/Symfony/Component/Serializer/composer.json src/Symfony/Component/Stopwatch/composer.json src/Symfony/Component/Templating/composer.json src/Symfony/Component/Translation/composer.json src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php src/Symfony/Component/Validator/composer.json src/Symfony/Component/VarDumper/composer.json src/Symfony/Component/Yaml/composer.json
2 parents e514cea + 2235a0d commit ebb9069

File tree

8 files changed

+80
-51
lines changed

8 files changed

+80
-51
lines changed

Extension/Validator/Constraints/FormValidator.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,39 @@ public function validate($form, Constraint $constraint)
9999
? (string) $form->getViewData()
100100
: gettype($form->getViewData());
101101

102-
$this->buildViolation($config->getOption('invalid_message'))
103-
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
104-
->setInvalidValue($form->getViewData())
105-
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
106-
->setCause($form->getTransformationFailure())
107-
->addViolation();
102+
if ($this->context instanceof ExecutionContextInterface) {
103+
$this->context->buildViolation($config->getOption('invalid_message'))
104+
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
105+
->setInvalidValue($form->getViewData())
106+
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
107+
->setCause($form->getTransformationFailure())
108+
->addViolation();
109+
} else {
110+
$this->buildViolation($config->getOption('invalid_message'))
111+
->setParameters(array_replace(array('{{ value }}' => $clientDataAsString), $config->getOption('invalid_message_parameters')))
112+
->setInvalidValue($form->getViewData())
113+
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
114+
->setCause($form->getTransformationFailure())
115+
->addViolation();
116+
}
108117
}
109118
}
110119

111120
// Mark the form with an error if it contains extra fields
112121
if (!$config->getOption('allow_extra_fields') && count($form->getExtraData()) > 0) {
113-
$this->buildViolation($config->getOption('extra_fields_message'))
114-
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
115-
->setInvalidValue($form->getExtraData())
116-
->setCode(Form::NO_SUCH_FIELD_ERROR)
117-
->addViolation();
122+
if ($this->context instanceof ExecutionContextInterface) {
123+
$this->context->buildViolation($config->getOption('extra_fields_message'))
124+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
125+
->setInvalidValue($form->getExtraData())
126+
->setCode(Form::NO_SUCH_FIELD_ERROR)
127+
->addViolation();
128+
} else {
129+
$this->buildViolation($config->getOption('extra_fields_message'))
130+
->setParameter('{{ extra_fields }}', implode('", "', array_keys($form->getExtraData())))
131+
->setInvalidValue($form->getExtraData())
132+
->setCode(Form::NO_SUCH_FIELD_ERROR)
133+
->addViolation();
134+
}
118135
}
119136
}
120137

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/Core/DataTransformer/DateTimeToStringTransformerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public function dataProvider()
5858

5959
// seconds since Unix
6060
array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
61+
62+
array('Y-z', '2010-33', '2010-02-03 00:00:00 UTC'),
6163
);
6264

6365
return $data;

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,12 @@ function () { throw new TransformationFailedException(); }
224224

225225
$this->validator->validate($form, new Form());
226226

227-
$is2Dot4Api = Validation::API_VERSION_2_4 === $this->getApiVersion();
228-
229227
$this->buildViolation('invalid_message_key')
230228
->setParameter('{{ value }}', 'foo')
231229
->setParameter('{{ foo }}', 'bar')
232230
->setInvalidValue('foo')
233231
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
234-
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
232+
->setCause($form->getTransformationFailure())
235233
->assertRaised();
236234
}
237235

@@ -261,14 +259,12 @@ function () { throw new TransformationFailedException(); }
261259

262260
$this->validator->validate($form, new Form());
263261

264-
$is2Dot4Api = Validation::API_VERSION_2_4 === $this->getApiVersion();
265-
266262
$this->buildViolation('invalid_message_key')
267263
->setParameter('{{ value }}', 'foo')
268264
->setParameter('{{ foo }}', 'bar')
269265
->setInvalidValue('foo')
270266
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
271-
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
267+
->setCause($form->getTransformationFailure())
272268
->assertRaised();
273269
}
274270

@@ -298,13 +294,11 @@ function () { throw new TransformationFailedException(); }
298294

299295
$this->validator->validate($form, new Form());
300296

301-
$is2Dot4Api = Validation::API_VERSION_2_4 === $this->getApiVersion();
302-
303297
$this->buildViolation('invalid_message_key')
304298
->setParameter('{{ value }}', 'foo')
305299
->setInvalidValue('foo')
306300
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
307-
->setCause($is2Dot4Api ? null : $form->getTransformationFailure())
301+
->setCause($form->getTransformationFailure())
308302
->assertRaised();
309303
}
310304

Tests/Extension/Validator/Constraints/LegacyFormValidator2Dot4ApiTest.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

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)