Permalink
Browse files

[Form] Allowed native framework errors to be mapped as well

  • Loading branch information...
webmozart committed May 21, 2012
1 parent 39bb151 commit d8ef71baf1963704d732ac9a2bb7c06ac8c87bea
View
@@ -83,3 +83,5 @@ CHANGELOG
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
+ * deprecated the option "validation_constraint" in favor of the new
+ option "constraints"
@@ -1,68 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form\Extension\Core\EventListener;
-
-use Symfony\Component\Form\FormError;
-use Symfony\Component\Form\FormEvents;
-use Symfony\Component\Form\Event\DataEvent;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
-/**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
-class ValidationListener implements EventSubscriberInterface
-{
- /**
- * {@inheritdoc}
- */
- static public function getSubscribedEvents()
- {
- return array(FormEvents::POST_BIND => 'validateForm');
- }
-
- public function validateForm(DataEvent $event)
- {
- $form = $event->getForm();
-
- if (!$form->isSynchronized()) {
- $form->addError(new FormError(
- $form->getAttribute('invalid_message'),
- $form->getAttribute('invalid_message_parameters')
- ));
- }
-
- if (count($form->getExtraData()) > 0) {
- $form->addError(new FormError('This form should not contain extra fields.'));
- }
-
- if ($form->isRoot() && isset($_SERVER['CONTENT_LENGTH'])) {
- $length = (int) $_SERVER['CONTENT_LENGTH'];
- $max = trim(ini_get('post_max_size'));
-
- if ('' !== $max) {
- switch (strtolower(substr($max, -1))) {
- // The 'G' modifier is available since PHP 5.1.0
- case 'g':
- $max *= 1024;
- case 'm':
- $max *= 1024;
- case 'k':
- $max *= 1024;
- }
-
- if ($length > $max) {
- $form->addError(new FormError('The uploaded file was too large. Please try to upload a smaller file'));
- }
- }
- }
- }
-}
@@ -51,8 +51,6 @@ public function buildForm(FormBuilder $builder, array $options)
'days',
'empty_value',
'required',
- 'invalid_message',
- 'invalid_message_parameters',
'translation_domain',
)));
$timeOptions = array_intersect_key($options, array_flip(array(
@@ -62,8 +60,6 @@ public function buildForm(FormBuilder $builder, array $options)
'with_seconds',
'empty_value',
'required',
- 'invalid_message',
- 'invalid_message_parameters',
'translation_domain',
)));
@@ -18,7 +18,6 @@
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
-use Symfony\Component\Form\Extension\Core\EventListener\ValidationListener;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Exception\FormException;
@@ -50,19 +49,15 @@ public function buildForm(FormBuilder $builder, array $options)
->setVirtual($options['virtual'])
->setAttribute('read_only', $options['read_only'])
->setAttribute('by_reference', $options['by_reference'])
- ->setAttribute('error_mapping', $options['error_mapping'])
->setAttribute('max_length', $options['max_length'])
->setAttribute('pattern', $options['pattern'])
->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
->setAttribute('attr', $options['attr'])
->setAttribute('label_attr', $options['label_attr'])
- ->setAttribute('invalid_message', $options['invalid_message'])
- ->setAttribute('invalid_message_parameters', $options['invalid_message_parameters'])
->setAttribute('translation_domain', $options['translation_domain'])
->setAttribute('single_control', $options['single_control'])
->setData($options['data'])
->setDataMapper(new PropertyPathMapper())
- ->addEventSubscriber(new ValidationListener())
;
if ($options['trim']) {
@@ -197,27 +192,24 @@ public function getDefaultOptions()
};
return array(
- 'data' => null,
- 'data_class' => $dataClass,
- 'empty_data' => $emptyData,
- 'trim' => true,
- 'required' => true,
- 'read_only' => false,
- 'disabled' => false,
- 'max_length' => null,
- 'pattern' => null,
- 'property_path' => null,
- 'mapped' => $mapped,
- 'by_reference' => true,
- 'error_bubbling' => $errorBubbling,
- 'error_mapping' => array(),
- 'label' => null,
- 'attr' => array(),
- 'label_attr' => array(),
- 'virtual' => false,
- 'single_control' => false,
- 'invalid_message' => 'This value is not valid.',
- 'invalid_message_parameters' => array(),
+ 'data' => null,
+ 'data_class' => $dataClass,
+ 'empty_data' => $emptyData,
+ 'trim' => true,
+ 'required' => true,
+ 'read_only' => false,
+ 'disabled' => false,
+ 'max_length' => null,
+ 'pattern' => null,
+ 'property_path' => null,
+ 'mapped' => $mapped,
+ 'by_reference' => true,
+ 'error_bubbling' => $errorBubbling,
+ 'label' => null,
+ 'attr' => array(),
+ 'label_attr' => array(),
+ 'virtual' => false,
+ 'single_control' => false,
'translation_domain' => 'messages',
);
}
@@ -42,11 +42,6 @@ public function buildForm(FormBuilder $builder, array $options)
*/
public function getDefaultOptions()
{
- // Map errors to the first field
- $errorMapping = function (Options $options) {
- return array('.' => $options['first_name']);
- };
-
return array(
'type' => 'text',
'options' => array(),
@@ -55,7 +50,6 @@ public function getDefaultOptions()
'first_name' => 'first',
'second_name' => 'second',
'error_bubbling' => false,
- 'error_mapping' => $errorMapping,
);
}
@@ -0,0 +1,33 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Extension\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @author Bernhard Schussek <bschussek@gmail.com>
+ */
+class Form extends Constraint
+{
+ /**
+ * Violation code marking an invalid form.
+ */
+ const ERR_INVALID = 1;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getTargets()
+ {
+ return self::CLASS_CONSTRAINT;
+ }
+}
Oops, something went wrong.

0 comments on commit d8ef71b

Please sign in to comment.