Nuevo en Symfony 5.1: Validador AtLeastOneOf

Además del validador Hostname y del validador ExpressionLanguage, en Symfony 5.1 hemos añadido otro validador llamado AtLeastOneOf. Puedes aplicarlo a propiedades y métodos para comprobar que sus valores cumplen al menos una de las restricciones indicadas:

namespace App\Entity;
 
use Symfony\Component\Validator\Constraints as Assert;
 
class SomeEntity
{
    /**
     * @Assert\AtLeastOneOf({
     *     @Assert\Length(min=5),
     *     @Assert\EqualTo("bar")
     * })
     */
    public $name = 'foo';
 
    /**
     * @Assert\AtLeastOneOf({
     *     @Assert\All({@Assert\GreaterThanOrEqual(10)}),
     *     @Assert\Count(20)
     * })
     */
    public $numbers = ['3', '5'];
 
    /**
     * @Assert\All({
     *     @Assert\AtLeastOneOf({
     *          @Assert\GreaterThanOrEqual(5),
     *          @Assert\LessThanOrEqual(3)
     *     })
     * })
     */
    public $otherNumbers = ['4', '5'];
}

Los mensajes de error por defecto incluyen la lista completa de restricciones que no se cumplieron:

name: This value should satisfy at least one of the following constraints:
[1] This value is too short. It should have 5 characters or more.
[2] This value should be equal to "bar".
 
numbers: This value should satisfy at least one of the following constraints:
[1] Each element of this collection should satisfy its own set of constraints.
[2] This collection should contain exactly 20 elements.
 
otherNumbers[0]: This value should satisfy at least one of the following constraints:
[1] This value should be greater than or equal to 5.
[2] This value should be less than or equal to 3.

Esta funcionalidad fue contribuida por Przemysław Bogusz en el pull request #35744.


Fuente: New in Symfony 5.1: AtLeastOne validator

Comentarios

Publicada el

13 de mayo de 2020

Etiquetas

Proyectos Symfony destacados

La forma más sencilla de generar el backend de tus aplicaciones Symfony. Ver más

Síguenos en @symfony_es para acceder a las últimas noticias.