Pārlūkot izejas kodu

[Form] added a way to specify the form constraint when building the form (useful if you work with arrays instead of objects)

Fabien Potencier 14 gadi atpakaļ
vecāks
revīzija
bee5d07d86

+ 2 - 0
src/Symfony/Component/Form/Type/FieldType.php

@@ -57,6 +57,7 @@ class FieldType extends AbstractType
             ->setAttribute('error_mapping', $options['error_mapping'])
             ->setAttribute('max_length', $options['max_length'])
             ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
+            ->setAttribute('validation_constraint', $options['validation_constraint'])
             ->setData($options['data'])
             ->addValidator(new DefaultValidator())
             ->addValidator(new DelegatingValidator($this->validator));
@@ -115,6 +116,7 @@ class FieldType extends AbstractType
             'error_bubbling' => false,
             'error_mapping' => array(),
             'label' => null,
+            'validation_constraint' => null,
         );
 
         if (!empty($options['data_class'])) {

+ 7 - 1
src/Symfony/Component/Form/Validator/DelegatingValidator.php

@@ -44,7 +44,13 @@ class DelegatingValidator implements FormValidatorInterface
             // Validate the form in group "Default"
             // Validation of the data in the custom group is done by validateData(),
             // which is constrained by the Execute constraint
-            if ($violations = $this->validator->validate($form)) {
+            if ($form->hasAttribute('validation_constraint')) {
+                $violations = $this->validator->validateValue($form->getData(), $form->getAttribute('validation_constraint'));
+            } else {
+                $violations = $this->validator->validate($form);
+            }
+
+            if ($violations) {
                 foreach ($violations as $violation) {
                     $propertyPath = $violation->getPropertyPath();
                     $template = $violation->getMessageTemplate();