55namespace PhpList \RestBundle \Identity \Request ;
66
77use PhpList \Core \Domain \Identity \Model \Dto \AdminAttributeDefinitionDto ;
8+ use PhpList \Core \Domain \Subscription \Validator \AttributeTypeValidator ;
89use PhpList \RestBundle \Common \Request \RequestInterface ;
10+ use Symfony \Component \Translation \IdentityTranslator ;
911use 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 ' )]
1116class 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