Skip to content

Constraint REGEX htmlPattern cannot be a boolean. #53807

Closed
@cavasinf

Description

@cavasinf

Symfony version(s) affected

6.4.3

Description

Doc says that we can pass false to htmlPattern parameter, but in reality this is not the case.

Setting htmlPattern to false will disable client side validation.

https://symfony.com/doc/6.4/reference/constraints/Regex.html#htmlpattern

Class:

https://github.com/symfony/validator/blob/03b0c75d7d3df1ef9a0fd9fb8db1e86f83ffa2bb/Constraints/Regex.php#L39

How to reproduce

  1. Add assert constraint to an entity:
use Symfony\Component\Validator\Constraints as Assert;

// ...

#[Assert\Regex(
    pattern: '/^[a-z]+(-[a-z]+)*$/',
    htmlPattern: false,
)]
private ?string $slug = null;
  1. Run phpstan
  2. Error
------ --------------------------------------------------------------------------------------------------------------------------------------- 
  Line   App/Entity/MyEntityphp                                                                                             
 ------ --------------------------------------------------------------------------------------------------------------------------------------- 
  33     Parameter $htmlPattern of attribute class Symfony\Component\Validator\Constraints\Regex constructor expects string|null, false given.  
 ------ ---------------------------------------------------------------------------------------------------------------------------------------

Possible Solution

Allow boolean or false type on htmlPattern parameter.

Additional Context

No response

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