Skip to content

Commit 14e6761

Browse files
[3.0] Cleaning Form and Validator a bit more
1 parent 4067a5c commit 14e6761

File tree

11 files changed

+33
-117
lines changed

11 files changed

+33
-117
lines changed

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,11 @@ public function configureOptions(OptionsResolver $resolver)
245245
return '';
246246
};
247247

248-
$placeholder = function (Options $options) {
248+
$placeholderDefault = function (Options $options) {
249249
return $options['required'] ? null : '';
250250
};
251251

252-
$choiceListNormalizer = function (Options $options, $choiceList) use ($choiceListFactory) {
253-
if ($choiceList) {
254-
@trigger_error('The "choice_list" option is deprecated since version 2.7 and will be removed in 3.0. Use "choice_loader" instead.', E_USER_DEPRECATED);
255-
256-
return $choiceList;
257-
}
258-
252+
$choiceListNormalizer = function (Options $options) use ($choiceListFactory) {
259253
if (null !== $options['choice_loader']) {
260254
return $choiceListFactory->createListFromLoader(
261255
$options['choice_loader'],
@@ -316,7 +310,7 @@ public function configureOptions(OptionsResolver $resolver)
316310
'preferred_choices' => array(),
317311
'group_by' => null,
318312
'empty_data' => $emptyData,
319-
'placeholder' => $placeholder,
313+
'placeholder' => $placeholderDefault,
320314
'error_bubbling' => false,
321315
'compound' => $compound,
322316
// The view data is always a string, even if the "data" option
@@ -330,7 +324,7 @@ public function configureOptions(OptionsResolver $resolver)
330324
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
331325
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
332326

333-
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface', 'Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'));
327+
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface'));
334328
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
335329
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
336330
$resolver->setAllowedTypes('choices_as_values', 'bool');

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ public function configureOptions(OptionsResolver $resolver)
159159
'empty_data' => $emptyData,
160160
'trim' => true,
161161
'required' => true,
162-
'max_length' => null,
163-
'pattern' => null,
164162
'property_path' => null,
165163
'mapped' => true,
166164
'by_reference' => true,

src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php

-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public static function getSubscribedEvents()
3535
return array(FormEvents::POST_SUBMIT => 'validateForm');
3636
}
3737

38-
/**
39-
* @param ValidatorInterface $validator
40-
* @param ViolationMapperInterface $violationMapper
41-
*/
4238
public function __construct(ValidatorInterface $validator, ViolationMapperInterface $violationMapper)
4339
{
4440
$this->validator = $validator;

src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php

-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class FormTypeValidatorExtension extends BaseValidatorExtension
3333
*/
3434
private $violationMapper;
3535

36-
/**
37-
* @param ValidatorInterface $validator
38-
*/
3936
public function __construct(ValidatorInterface $validator)
4037
{
4138
$this->validator = $validator;

src/Symfony/Component/Form/FormFactory.php

+3-31
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,11 @@ public function createForProperty($class, $property, $data = null, array $option
6161
*/
6262
public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = array())
6363
{
64-
$name = null;
65-
$typeName = null;
66-
67-
if ($type instanceof ResolvedFormTypeInterface) {
68-
if (method_exists($type, 'getBlockPrefix')) {
69-
// As of Symfony 3.0, the block prefix of the type is used as
70-
// default name
71-
$name = $type->getBlockPrefix();
72-
} else {
73-
// BC
74-
$typeName = $type->getName();
75-
}
76-
} elseif ($type instanceof FormTypeInterface) {
77-
// BC
78-
$typeName = $type->getName();
79-
} elseif (is_string($type)) {
80-
// BC
81-
$typeName = $type;
82-
} else {
83-
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
84-
}
85-
86-
if (null === $name) {
87-
if (false === strpos($typeName, '\\')) {
88-
// No FQCN - leave unchanged for BC
89-
$name = $typeName;
90-
} else {
91-
// FQCN
92-
$name = StringUtil::fqcnToBlockPrefix($typeName);
93-
}
64+
if (!is_string($type)) {
65+
throw new UnexpectedTypeException($type, 'string');
9466
}
9567

96-
return $this->createNamedBuilder($name, $type, $data, $options);
68+
return $this->createNamedBuilder(StringUtil::fqcnToBlockPrefix($type), $type, $data, $options);
9769
}
9870

9971
/**

src/Symfony/Component/Form/FormFactoryInterface.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ interface FormFactoryInterface
2121
*
2222
* @see createBuilder()
2323
*
24-
* @param string|FormTypeInterface $type The type of the form
25-
* @param mixed $data The initial data
26-
* @param array $options The options
24+
* @param string $type The type of the form
25+
* @param mixed $data The initial data
26+
* @param array $options The options
2727
*
2828
* @return FormInterface The form named after the type
2929
*
@@ -36,10 +36,10 @@ public function create($type = 'Symfony\Component\Form\Extension\Core\Type\FormT
3636
*
3737
* @see createNamedBuilder()
3838
*
39-
* @param string|int $name The name of the form
40-
* @param string|FormTypeInterface $type The type of the form
41-
* @param mixed $data The initial data
42-
* @param array $options The options
39+
* @param string|int $name The name of the form
40+
* @param string $type The type of the form
41+
* @param mixed $data The initial data
42+
* @param array $options The options
4343
*
4444
* @return FormInterface The form
4545
*
@@ -66,9 +66,9 @@ public function createForProperty($class, $property, $data = null, array $option
6666
/**
6767
* Returns a form builder.
6868
*
69-
* @param string|FormTypeInterface $type The type of the form
70-
* @param mixed $data The initial data
71-
* @param array $options The options
69+
* @param string $type The type of the form
70+
* @param mixed $data The initial data
71+
* @param array $options The options
7272
*
7373
* @return FormBuilderInterface The form builder
7474
*
@@ -79,10 +79,10 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ
7979
/**
8080
* Returns a form builder.
8181
*
82-
* @param string|int $name The name of the form
83-
* @param string|FormTypeInterface $type The type of the form
84-
* @param mixed $data The initial data
85-
* @param array $options The options
82+
* @param string|int $name The name of the form
83+
* @param string $type The type of the form
84+
* @param mixed $data The initial data
85+
* @param array $options The options
8686
*
8787
* @return FormBuilderInterface The form builder
8888
*
@@ -93,7 +93,7 @@ public function createNamedBuilder($name, $type = 'Symfony\Component\Form\Extens
9393
/**
9494
* Returns a form builder for a property of a class.
9595
*
96-
* If any of the 'max_length', 'required' and type options can be guessed,
96+
* If any of the 'required' and type options can be guessed,
9797
* and are not provided in the options argument, the guessed value is used.
9898
*
9999
* @param string $class The fully qualified class name

src/Symfony/Component/Form/Tests/FormFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testCreateNamedBuilderThrowsUnderstandableException()
162162

163163
/**
164164
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
165-
* @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
165+
* @expectedExceptionMessage Expected argument of type "string", "stdClass" given
166166
*/
167167
public function testCreateThrowsUnderstandableException()
168168
{
@@ -182,7 +182,7 @@ public function testCreateUsesTypeNameIfTypeGivenAsString()
182182

183183
$resolvedType->expects($this->once())
184184
->method('createBuilder')
185-
->with($this->factory, 'TYPE', $options)
185+
->with($this->factory, 'type', $options)
186186
->will($this->returnValue($this->builder));
187187

188188
$this->builder->expects($this->any())

src/Symfony/Component/Form/Util/InheritDataAwareIterator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getChildren()
3434
}
3535

3636
/**
37-
*{@inheritdoc}
37+
* {@inheritdoc}
3838
*/
3939
public function hasChildren()
4040
{

src/Symfony/Component/Validator/Mapping/TraversalStrategy.php

-10
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ class TraversalStrategy
4848
*/
4949
const TRAVERSE = 4;
5050

51-
/**
52-
* Specifies that nested instances of {@link \Traversable} should never be
53-
* iterated. Can be combined with {@link IMPLICIT} or {@link TRAVERSE}.
54-
*
55-
* @deprecated since version 2.5, to be removed in 3.0. This constant was added for backwards compatibility only.
56-
*
57-
* @internal
58-
*/
59-
const STOP_RECURSION = 8;
60-
6151
/**
6252
* Not instantiable.
6353
*/

src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php

+8-27
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public function validate($value, $constraints = null, $groups = null)
167167
$value,
168168
$this->defaultPropertyPath,
169169
$groups,
170-
true,
171170
$this->context
172171
);
173172

@@ -378,7 +377,6 @@ private function validateObject($object, $propertyPath, array $groups, $traversa
378377
$object,
379378
$propertyPath,
380379
$groups,
381-
$traversalStrategy & TraversalStrategy::STOP_RECURSION,
382380
$context
383381
);
384382
}
@@ -392,26 +390,16 @@ private function validateObject($object, $propertyPath, array $groups, $traversa
392390
* objects are iterated as well. Nested arrays are always iterated,
393391
* regardless of the value of $recursive.
394392
*
395-
* @param array|\Traversable $collection The collection
396-
* @param string $propertyPath The current property path
397-
* @param string[] $groups The validated groups
398-
* @param bool $stopRecursion Whether to disable
399-
* recursive iteration. For
400-
* backwards compatibility
401-
* with Symfony < 2.5.
402-
* @param ExecutionContextInterface $context The current execution context
393+
* @param array|\Traversable $collection The collection
394+
* @param string $propertyPath The current property path
395+
* @param string[] $groups The validated groups
396+
* @param ExecutionContextInterface $context The current execution context
403397
*
404398
* @see ClassNode
405399
* @see CollectionNode
406400
*/
407-
private function validateEachObjectIn($collection, $propertyPath, array $groups, $stopRecursion, ExecutionContextInterface $context)
401+
private function validateEachObjectIn($collection, $propertyPath, array $groups, ExecutionContextInterface $context)
408402
{
409-
if ($stopRecursion) {
410-
$traversalStrategy = TraversalStrategy::NONE;
411-
} else {
412-
$traversalStrategy = TraversalStrategy::IMPLICIT;
413-
}
414-
415403
foreach ($collection as $key => $value) {
416404
if (is_array($value)) {
417405
// Arrays are always cascaded, independent of the specified
@@ -421,7 +409,6 @@ private function validateEachObjectIn($collection, $propertyPath, array $groups,
421409
$value,
422410
$propertyPath.'['.$key.']',
423411
$groups,
424-
$stopRecursion,
425412
$context
426413
);
427414

@@ -435,7 +422,7 @@ private function validateEachObjectIn($collection, $propertyPath, array $groups,
435422
$value,
436423
$propertyPath.'['.$key.']',
437424
$groups,
438-
$traversalStrategy,
425+
TraversalStrategy::IMPLICIT,
439426
$context
440427
);
441428
}
@@ -613,9 +600,7 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
613600
// If no specific traversal strategy was requested when this method
614601
// was called, use the traversal strategy of the class' metadata
615602
if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
616-
// Keep the STOP_RECURSION flag, if it was set
617-
$traversalStrategy = $metadata->getTraversalStrategy()
618-
| ($traversalStrategy & TraversalStrategy::STOP_RECURSION);
603+
$traversalStrategy = $metadata->getTraversalStrategy();
619604
}
620605

621606
// Traverse only if IMPLICIT or TRAVERSE
@@ -643,7 +628,6 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
643628
$object,
644629
$propertyPath,
645630
$groups,
646-
$traversalStrategy & TraversalStrategy::STOP_RECURSION,
647631
$context
648632
);
649633
}
@@ -729,9 +713,7 @@ private function validateGenericNode($value, $object, $cacheKey, MetadataInterfa
729713
// If no specific traversal strategy was requested when this method
730714
// was called, use the traversal strategy of the node's metadata
731715
if ($traversalStrategy & TraversalStrategy::IMPLICIT) {
732-
// Keep the STOP_RECURSION flag, if it was set
733-
$traversalStrategy = $metadata->getTraversalStrategy()
734-
| ($traversalStrategy & TraversalStrategy::STOP_RECURSION);
716+
$traversalStrategy = $metadata->getTraversalStrategy();
735717
}
736718

737719
// The $cascadedGroups property is set, if the "Default" group is
@@ -749,7 +731,6 @@ private function validateGenericNode($value, $object, $cacheKey, MetadataInterfa
749731
$value,
750732
$propertyPath,
751733
$cascadedGroups,
752-
$traversalStrategy & TraversalStrategy::STOP_RECURSION,
753734
$context
754735
);
755736

src/Symfony/Component/Validator/Validator/RecursiveValidator.php

-12
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\Validator\Validator;
1313

1414
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\Constraints\GroupSequence;
16-
use Symfony\Component\Validator\Constraints\Valid;
1715
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
1816
use Symfony\Component\Validator\Context\ExecutionContextFactoryInterface;
1917
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -141,14 +139,4 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
141139
->validatePropertyValue($objectOrClass, $propertyName, $value, $groups)
142140
->getViolations();
143141
}
144-
145-
private static function testConstraints($constraints)
146-
{
147-
return null === $constraints || $constraints instanceof Constraint || (is_array($constraints) && (0 === count($constraints) || current($constraints) instanceof Constraint));
148-
}
149-
150-
private static function testGroups($groups)
151-
{
152-
return null === $groups || is_string($groups) || $groups instanceof GroupSequence || (is_array($groups) && (0 === count($groups) || is_string(current($groups)) || current($groups) instanceof GroupSequence));
153-
}
154142
}

0 commit comments

Comments
 (0)