I could really use a helping hand here
I tend to create projects with a controller/action where I need validation. I have a number of possibilities
- Create a DTO object (not a entity) with validation annotations
- Use my Model object, where each property has validation rules annotated (I can even add ValidationGroups)
- Annotate each action argument with it’s own validation and create my final entity with the arguments
I’ve tried all three.
Number 1 is very explicit according to the current context and usecase, so that is great. But leads to many small DTO classes and tend to get a mess for me (I would love to see how other organizes it, if you do this - PLEASE SHARE )
Lately I’ve been going away from number 2 (use the model) since all validation rules might not apply.
And number 3 can end up in a large docblock or custom (/combined) validators for a explicit argument (ValidPasswordValidator, ValidAccountIdentifierValidator). Just so we are on the same pace, this is how I’ved used it
<?php
namespace Vendor\Core\Validation\Validator;
use Neos\Flow\Validation\Validator\ConjunctionValidator;
class ValidAccountIdentifierValidator extends ConjunctionValidator
{
public function validate($value)
{
$this->addValidator(new NotEmptyValidator());
$this->addValidator(new EmailAddressValidator());
$this->addValidator(new UniqueEmailAddressValidator());
return parent::validate($value);
}
}
Where as in a API context (one action = one endpoint) it makes sense to go for some kind of DTO approach. But then more validation concepts get’s mixed. And I tend to get in doubt what is really the best way
I would love to hear how you do, usescases, best practice, experiences and such.
Al in the name of getting better at using Flow and learn from others.
I hope you will share
Søren