Skip to content

Commit 36100ea

Browse files
committed
Fix AdminAttributeDefinitionRequest choice
1 parent 3570df9 commit 36100ea

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Identity/Request/AdminAttributeDefinitionRequest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
namespace PhpList\RestBundle\Identity\Request;
66

77
use PhpList\Core\Domain\Identity\Model\Dto\AdminAttributeDefinitionDto;
8+
use PhpList\Core\Domain\Subscription\Validator\AttributeTypeValidator;
89
use PhpList\RestBundle\Common\Request\RequestInterface;
10+
use Symfony\Component\Translation\IdentityTranslator;
911
use Symfony\Component\Validator\Constraints as Assert;
12+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
13+
use Symfony\Component\Validator\Exception\ValidatorException;
1014

15+
#[Assert\Callback('validateType')]
1116
class AdminAttributeDefinitionRequest implements RequestInterface
1217
{
1318
#[Assert\NotBlank]
1419
public string $name;
1520

21+
#[Assert\Choice(choices: ['hidden', 'textline'], message: 'Invalid type. Allowed values: hidden, textline.')]
1622
public ?string $type = null;
23+
1724
public ?int $order = null;
25+
1826
public ?string $defaultValue = null;
27+
1928
public bool $required = false;
2029

2130
public function getDto(): AdminAttributeDefinitionDto
@@ -28,4 +37,21 @@ public function getDto(): AdminAttributeDefinitionDto
2837
required: $this->required,
2938
);
3039
}
40+
41+
public function validateType(ExecutionContextInterface $context): void
42+
{
43+
if ($this->type === null) {
44+
return;
45+
}
46+
47+
$validator = new AttributeTypeValidator(new IdentityTranslator());
48+
49+
try {
50+
$validator->validate($this->type);
51+
} catch (ValidatorException $e) {
52+
$context->buildViolation($e->getMessage())
53+
->atPath('type')
54+
->addViolation();
55+
}
56+
}
3157
}

0 commit comments

Comments
 (0)