The Form and Validator components are one of the largest Symfony components. In Symfony 2.7 we improved them by adding new features and deprecating some existing features.

Added checkDNS option to URL validator

Sarah Khalil
Contributed by Sarah Khalil in #12956

The Url constraint validates that the given value is a valid URL string. In Symfony 2.7 a new checkDNS option has been added to also check whether the host referred in the URL is valid:

1
2
3
4
5
6
7
8
9
10
11
namespace AppBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Website
{
    /**
     * @Assert\Url(checkDNS = true)
     */
     protected $url;
}

Internally this option executes the following PHP code:

1
2
3
4
$host = parse_url($value, PHP_URL_HOST);
if (!checkdnsrr($host, 'ANY')) {
    // ... error
}

Renamed the precision option to scale

Wouter De Jong
Contributed by Wouter De Jong in #13717

The precision option of the Number field type is a locale-specific setting that specifies how many decimals are allowed until the field rounds the submitted value. In Symfony 2.7 this option has been renamed to scale:

1
2
3
4
5
// Symfony 2.6
$builder->add('length', 'number', array('precision' => 3));

// Symfony 2.7
$builder->add('length', 'number', array('scale' => 3));

Deprecated setDefaultOptions() in favor of configureOptions()

Peter Rehm
Contributed by Peter Rehm in #12891

In Symfony 2.7, the setDefaultOptions() method of AbstractType and AbstractExtensionType has been deprecated in favor of the new configureOptions() method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Symfony 2.6
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TaskType extends AbstractType
{
    // ...
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Task',
        ));
    }
}

// Symfony 2.7
use Symfony\Component\OptionsResolver\OptionsResolver;

class TaskType extends AbstractType
{
    // ...
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Task',
        ));
    }
}

Added support for importing validation constraints from subdirectories

Jérôme Tamarelle
Contributed by Jérôme Tamarelle in #13855

If your bundles contain a lot of entities or documents and you define their validation using YAML or XML format instead of annotations, the result may be a validation file too large to manage.

Symfony 2.7 adds support for loading any number of validation files from the Resources/config/validation/ subdirectory of your bundles:

1
2
3
4
5
6
7
8
9
your-bundle/
├── ...
└── Resources/
    └── config/
         └── validation
              ├── Author.yml
              ├── Category.yml
              ├── Comment.yml
              └── Post.yml

Added choice_translation_domain domain to avoid translating options

Abdellatif Ait boudad
Contributed by Abdellatif Ait boudad in #13651

In Symfony 2.7, the ability to translate Doctrine type entries by the translator component is disabled by default, which results in a noticeable performance improvement. Enable this feature explicitly by setting to true the value of the new choice_translation_domain option:

1
2
3
4
5
6
7
8
9
10
// Symfony 2.6
$form->add('products', 'entity', array(
    'class' => 'AppBundle/Entity/Product',
));

// Symfony 2.7
$form->add('products', 'entity', array(
    'class' => 'AppBundle/Entity/Product',
    'choice_translation_domain' => true,
));

The values of the choice_translation_domain option can be true (reuse the current translation domain), false (disable translation), null (uses the parent translation domain or the default domain) and a string which represents the exact translation domain to use.

Published in #Living on the edge