Check Validation Constraints for Class Using Console Command in Symfony 7

Check Validation Constraints for Class Using Console Command in Symfony 7

Symfony framework provides many built-in commands that allow to debug various application parts. This tutorial explains how to check validation constraints for class using console command in Symfony 7 application.

Let's say we have the following class which has one property with two validation constraints:

src/Entity/User.php

<?php

namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    #[Assert\NotBlank]
    #[Assert\Length(min: 5)]
    private string $username;

    public function getUsername(): string { return $this->username; }
    public function setUsername(string $username): void { $this->username = $username; }
}

The debug:validator command can be used to get a list of validation constraints of a given class.

php bin/console debug:validator App\Entity\User

Command prints information in the table:

App\Entity\User 
---------------

+----------+--------------------------------------------------+---------------+--------------------------------------------------+
| Property | Name                                             | Groups        | Options                                          |
+----------+--------------------------------------------------+---------------+--------------------------------------------------+
| username | Symfony\Component\Validator\Constraints\NotBlank | Default, User | [                                                |
|          |                                                  |               |   "allowNull" => false,                          |
|          |                                                  |               |   "message" => "This value should not be blank.",|
|          |                                                  |               |   "normalizer" => null,                          |
|          |                                                  |               |   "payload" => null                              |
|          |                                                  |               | ]                                                |
| username | Symfony\Component\Validator\Constraints\Length   | Default, User | [                                                |
|          |                                                  |               |   "charset" => "UTF-8",                          |
|          |                                                  |               |   ...                                            |
|          |                                                  |               | ]                                                |
+----------+--------------------------------------------------+---------------+--------------------------------------------------+

We can provide directory name as argument to check validation constraints of all classes stored in that directory.

php bin/console debug:validator src/Entity

Leave a Comment

Cancel reply

Your email address will not be published.