فهرست منبع

[Form] Make the default invalid message translatable

Victor Berchet 14 سال پیش
والد
کامیت
30d348d18d

+ 4 - 0
src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

@@ -54,6 +54,8 @@ class FieldType extends AbstractType
             ->setAttribute('pattern', $options['pattern'])
             ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
             ->setAttribute('attr', $options['attr'] ?: array())
+            ->setAttribute('invalid_message_template', $options['invalid_message_template'])
+            ->setAttribute('invalid_message_parameters', $options['invalid_message_parameters'])
             ->setData($options['data'])
             ->addValidator(new DefaultValidator())
         ;
@@ -123,6 +125,8 @@ class FieldType extends AbstractType
             'error_mapping'     => array(),
             'label'             => null,
             'attr'              => array(),
+            'invalid_message_template'   => 'This value is not valid',
+            'invalid_message_parameters' => array(),
         );
 
         $class = isset($options['data_class']) ? $options['data_class'] : null;

+ 4 - 1
src/Symfony/Component/Form/Extension/Core/Validator/DefaultValidator.php

@@ -20,7 +20,10 @@ class DefaultValidator implements FormValidatorInterface
     public function validate(FormInterface $form)
     {
         if (!$form->isSynchronized()) {
-            $form->addError(new FormError(sprintf('The value for "%s" is invalid', $form->getName())));
+            $form->addError(new FormError(
+                $form->getAttribute('invalid_message_template'),
+                $form->getAttribute('invalid_message_parameters')
+            ));
         }
 
         if (count($form->getExtraData()) > 0) {