jsonapi-bundle
jsonapi-bundle copied to clipboard
Creating entities
Hi @paknahad,
I can see that ValidatorTrait
validate fields using doctrine
fields name, is not accurate to use getAttributeHydrator
return fields
https://github.com/paknahad/jsonapi-bundle/blob/d09115cc0ab0d4eb08a631d7b7d759734425f615/src/Hydrator/ValidatorTrait.php#L85
I mean, when I create object (POST
on /book
in my https://github.com/waghanza/book-manager), fields (in data
on my json) are validated according to what doctrine know, not what hydratator know
Regards,
Hi @waghanza
This bundle makes the hydrator's fields based on doctrine fields, so both of them are same. Once you need to change the field name in hydrator, you can set the "validExistance" equal false and write a validator based on your needs.
Hi,
@paknahad Where could you set validExistance to false ?
Regards,
in this line https://github.com/waghanza/book-manager/blob/7158bf8d6e9a76f9aaec138178b62ee265fb284c/src/JsonApi/Hydrator/Book/AbstractBookHydrator.php#L66 you can pass "false" to validator like this:
$this->validateFields($this->objectManager->getClassMetadata(Book::class), $request, false);
but in this case the field type could not determined in https://github.com/paknahad/jsonapi-bundle/blob/d09115cc0ab0d4eb08a631d7b7d759734425f615/src/Hydrator/ValidatorTrait.php#L89
yeah. on changing the name of a field( or adding a field to hydrator which doesn't exist in the entity), you can't expect to validate automatically. so I wrote in my first comment, you must validate this fields by a custom validator.
but how we can add our custom validator ?
In generated AbstractHydrator at the "validateRequest" method, after or instead of this line https://github.com/waghanza/book-manager/blob/7158bf8d6e9a76f9aaec138178b62ee265fb284c/src/JsonApi/Hydrator/Book/AbstractBookHydrator.php#L66
actually, you are able to change any line of generated codes based on your needs
@paknahad I see, but how about having generators that could be customized ? I mean instead creating something like a custom recipe
For example, we can use a optional param to make:api
to specify a template folder, with already customized template
What do you think ?
Sounds nice. but I'm so busy currently. if you could implement this feature, I would be glad to merge your PR.