Closed
Description
The way to use a ValidationGroupsGenerator descirbed in the documentation is no longer valid.
ApiPlatform version : "^2.7"
<?php
// api/src/Entity/Book.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Validator\AdminGroupsGenerator;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(validationContext: ['groups' => [AdminGroupsGenerator::class]])
class Book
{
#[Assert\NotBlank(groups: ['a'])]
public $name;
#[Assert\NotNull(groups: ['b'])]
public $author;
// ...
New way:
<?php
// api/src/Entity/Book.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Validator\AdminGroupsGenerator;
use Symfony\Component\Validator\Constraints as Assert;
// ----------------Changes are here --------------------
#[ApiResource(validationContext: ['groups' => AdminGroupsGenerator::class])
// ----------------Changes are here --------------------
class Book
{
#[Assert\NotBlank(groups: ['a'])]
public $name;
#[Assert\NotNull(groups: ['b'])]
public $author;
// ...
Doing otherwise will result on ApiPlatform not to invoke the AdminGroupsGenerator::class but use its fully qualified class name instead as a validation group (see Validator code bellow)
// ApiPlatform\Symfony\Validator\Validator
// ...
public function validate($data, array $context = [])
{
if (null !== $validationGroups = $context['groups'] ?? null) {
if (
$this->container &&
\is_string($validationGroups) &&
$this->container->has($validationGroups) &&
($service = $this->container->get($validationGroups)) &&
\is_callable($service)
) {
// ...
}
// ...
}
// Old documentation :
\is_string($validationGroups) // false, because $validationGroups is array
// New documentation :
\is_string($validationGroups) // true, because $validationGroups is string
Metadata
Metadata
Assignees
Labels
No labels