Skip to content

Commit

Permalink
[Form] Allowed native framework errors to be mapped as well
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed May 22, 2012
1 parent 59d6b55 commit ac69394
Show file tree
Hide file tree
Showing 25 changed files with 1,481 additions and 745 deletions.
20 changes: 20 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -539,6 +539,26 @@
$form->getConfig()->getErrorBubbling();
```

* The option "validation_constraint" is deprecated and will be removed
in Symfony 2.3. You should use the option "constraints" instead,
where you can pass one or more constraints for a form.

Before:

```
$builder->add('name', 'text', array(
'validation_constraint' => new NotBlank(),
));
```

After (if the address object is an array):

```
$builder->add('name', 'text', array(
'constraints' => new NotBlank(),
));
```

### Validator

* The methods `setMessage()`, `getMessageTemplate()` and
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Expand Up @@ -133,9 +133,12 @@
</service>

<!-- FormTypeValidatorExtension -->
<service id="form.type_extension.field" class="Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension">
<service id="form.type_extension.form.validator" class="Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension">
<tag name="form.type_extension" alias="form" />
<argument type="service" id="validator" />
</service>
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
<tag name="form.type_extension" alias="repeated" />
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -83,3 +83,5 @@ CHANGELOG
* `getErrorBubbling`
* `getNormTransformers`
* `getClientTransformers`
* deprecated the option "validation_constraint" in favor of the new
option "constraints"

This file was deleted.

Expand Up @@ -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(
Expand All @@ -62,8 +60,6 @@ public function buildForm(FormBuilder $builder, array $options)
'with_seconds',
'empty_value',
'required',
'invalid_message',
'invalid_message_parameters',
'translation_domain',
)));

Expand Down
44 changes: 18 additions & 26 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -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',
);
}
Expand Down
Expand Up @@ -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(),
Expand All @@ -55,7 +50,6 @@ public function getDefaultOptions()
'first_name' => 'first',
'second_name' => 'second',
'error_bubbling' => false,
'error_mapping' => $errorMapping,
);
}

Expand Down
@@ -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;
}
}

0 comments on commit ac69394

Please sign in to comment.