Version
4.37.1
Question
I am using DTOs to model and validate my requests using my own bundle (crtl/request-dto-resolver-bundle.
I often have similar DTOs for example for create and update requests where some fields are optional in update but required in create. Using just symfony I can simply extend the class and override validation constriants using loadValidatorMetadata:
#[RequestDTO]
class MyUpdateDto {
/**
* @var mixed|string|null
*/
#[Assert\Length(max: 50)]
public mixed $name = null;
}
#[RequestDTO]
class MyCreateDto extends MyUpdateDto {
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint("name", new Assert\NotBlank());
}
}
It works fine in Symfony. name is required in MyCreateDto but optional in MyUpdateDto.
The problem is that API doc does not pick up loadValidatorMetadata (because its runtime).
So to override the validation constraints I have to override the complete property which is very cumbersome:
#[RequestDTO]
class MyCreateDto extends MyUpdateDto {
/**
* @var mixed|string|null
*/
#[Assert\NotBlank, Assert\Length(max: 50)]
public mixed $name = null;
}
Is there a way to extend properties from parent classes in child classes without redefining all the options?
Additional context
No response