New in Symfony 4.1: Validator improvements

Deprecated the checkDNS option of the URL validator

Contributed by
Roland Franssen
in #25516.

In Symfony 4.1, the checkDNS option (and its related dnsMessage option) of the Url constraint has been deprecated. This option allowed to check whether the host associated with the given URL existed. It used the checkdnsrr() PHP function and it's been deprecated (and removed in Symfony 5.0) because its results are not fully reliable.

No alternative is provided, so if you still want to apply this validation, create a custom validation and use the checkdnsrr() PHP function.

Allow to pass custom values to Expression validator

Contributed by
Gabriel Ostrolucký
in #25504.

In Symfony 4.1, the Expression constraint accepts a new option called values to pass arbitrary values and use them in your expressions:

1
2
3
4
5
6
7
8
use Symfony\Component\Validator\Constraints\Expression;

$constraint = new Expression([
    'expression' => 'value + custom == 2',
    'values' => [
        'custom' => 1,
    ],
]);

Added a canonicalize option to the Locale validator

Contributed by
Javier Spagnoletti
in #22353.

In Symfony 4.1, the Locale constraint defines a new boolean option called canonicalize. If true, the given locale value is transformed into its canonical form before validating it.

For example, FR-fr.utf8 is transformed into fr_FR, UZ-cYRL-uz is transformed into uz_Cyrl_UZ, etc.

1
2
3
4
5
6
7
use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /** @Assert\Locale(canonicalize = true) */
     protected $locale;
}

Added support for validating URLs without protocol

Contributed by
Peter Smeets
in #24308.

The Url constraint defines the protocols option to configure the protocols allowed for the URLs (['http', 'https'] by default). In Symfony 4.1 we added a new boolean option called relativeProtocol. If true, URLs without protocol (e.g. //example.com) are considered valid too:

1
2
3
4
5
6
7
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    /** @Assert\Url(relativeProtocol = true) */
     protected $bioUrl;
}

Comments

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.