# Example of symfony/validator integration in SlimPHP

**URL:** <https://discourse.slimframework.com/t/example-of-symfony-validator-integration-in-slimphp/6139>\
**Category:** Questions\
**Created:** [June 5, 2025, 10:05am UTC](https://discourse.slimframework.com/t/example-of-symfony-validator-integration-in-slimphp/6139 "2025-06-05T10:05:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bezlepkin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/bezlepkin/32/1653_2.png) [@Bezlepkin](https://discourse.slimframework.com/u/Bezlepkin)\
**Post date:** [June 5, 2025, 10:05am UTC](https://discourse.slimframework.com/t/example-of-symfony-validator-integration-in-slimphp/6139/1 "2025-06-05T10:05:30Z")

</div>

Hi! Does anyone have an example of how to integrate symfony/validator into SlimPHP using DTO classes?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [June 5, 2025, 7:38pm UTC](https://discourse.slimframework.com/t/example-of-symfony-validator-integration-in-slimphp/6139/2 "2025-06-05T19:38:39Z")

</div>

Hi! As far as I know the Symfony validator only supports arrays for validation.

Here is a simple example:

```php

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validation;

$array = [
    'title' => 'Test',
    'count' => 0,
    'demo' => ['one', 'two', 'three'],
    'box' => true,
];

$validator = Validation::createValidator();

$constraints = new Assert\Collection(
    [
        'title' => new Assert\Length(['min' => 6]),
        'count' => new Assert\Type('integer'),
        'demo' => new Assert\Type('array'),
        'box' => new Assert\Type('boolean'),
    ]
);

$violations = $validator->validate($array, $constraints);

if ($violations->count()) {
    throw new ValidationFailedException('Please check your input', $violations);
}

```

If you try to pass an object it will not work:

```php
class ExampleDto
{
    public string $title;
    public int $count;
    public array $demo;
    public bool $box;
}

$dto = new ExampleDto();
$dto->title = 'Test';
$dto->demo = ['one', 'two', 'three'];
$dto->box = true;
$dto->count = 1;

// ...

// Exception: This value should be of type array|(Traversable&ArrayAccess).
$violations = $validator->validate($dto, $constraints); 

```

---

<div class="post-metadata">

**Author:** ![tj\_gumis](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@tj\_gumis](https://discourse.slimframework.com/u/tj_gumis)\
**Post date:** [June 5, 2025, 8:37pm UTC](https://discourse.slimframework.com/t/example-of-symfony-validator-integration-in-slimphp/6139/3 "2025-06-05T20:37:32Z")

</div>

> Hi! As far as I know the Symfony validator only supports arrays for validation.

Actually it is not true.

Consider your validation middleware:

```php
use Symfony\Component\Validator\Validation;

        $validator = Validation::createValidatorBuilder()
            ->addMethodMapping('loadValidatorMetadata')
            ->getValidator();
        $errors = $validator->validate($dto);

```

And your `dto`:

```php
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Dto
{
    private ?int $userId = null;

    public function getUserId(): ?int
    {
        return $this->userId;
    }

    public function setUserId(?int $userId): void
    {
        $this->userId = $userId;
    }

    /**
     * @return array<int, mixed>
     */
    protected static function constraintsUserId(): array
    {
        return [
            new Constraints\Type('int'),
        ];
    }    
    /**
     * @var string[] $fieldsByModes
     */
    protected static array $fieldsByModes = [
        'userId',
    ];

    public static function loadValidatorMetadata(ClassMetadata $metadata): void
    {
        foreach (self::$fieldsByModesas as $field) {
            $metadata->addPropertyConstraints($field, self::{'constraints' . ucfirst($field)}());
        }
    }
}

```

Should work like a charm.
