Skip to content

[Validator] Support logical constraints/expression language #5749

Closed
@webmozart

Description

@webmozart

I would like to see logical constraints or an expression language to be supported by the validator.

<?php
class Entity
{
    /** @Or({ @Equals(10), @LessThan(10) }) */
    public $property;
}

The problem here is that "and", "or" and "xor" are PHP keywords, so we need to find a different name for these constraints:

  • or:
    • LOr (logical or)
    • AnyOf
    • Any
    • ... ?
  • and
    • LAnd
    • AllOf
    • All (not BC! redefines current All constraint)
    • ... ?
  • xor
    • LXor
    • EitherOf
    • Either
    • ... ?
  • not
    • Not
    • LNot
    • ... ?

An alternative approach is to introduce an expression language:

<?php
class Entity
{
    Alternative 1:
    /** @Assert\Expr({@Assert\Min(10), "or", @Assert\Type("integer")}) */
    public $property1;

    Alternative 2:
    /** @Assert\Expr("@Assert\Min(10) or @Assert\Type('integer')") */
    public $property2;
}

I don't know if Alternative 2 is feasible at all, because the annotation parser would have to be run on the expression with the scope of the file where the expression is defined. This is impossible to determine (the expression could be assembled dynamically and used to construct an Expr constraint at runtime).

P.S.: If "Any" and "All" are our best guesses, a solution would be to only introduce "Any" for now, deprecate "All" and

  1. merge it into "Collection", or
  2. introduce a new name "Each"

Once the deprecation phase of "All" is over (2.3?) we can remove it and introduce a new "All" constraint with the "and" semantics.

<?php
class Entity
{
    Alternative 1:
    /**
     * @Assert\Collection(each = {
     *     @Assert\Min(10),
     *     @Assert\Type("integer"),
     * })
     */
    public $property1;

    Alternative 2:
    /**
     * @Assert\Each({
     *     @Assert\Min(10),
     *     @Assert\Type("integer"),
     * })
     */
    public $property2;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions