jsonapi-bundle icon indicating copy to clipboard operation
jsonapi-bundle copied to clipboard

Creating entities

Open waghanza opened this issue 6 years ago • 9 comments

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,

waghanza avatar Nov 09 '18 16:11 waghanza

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.

paknahad avatar Nov 11 '18 08:11 paknahad

Hi,

@paknahad Where could you set validExistance to false ?

Regards,

waghanza avatar Nov 11 '18 12:11 waghanza

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);

paknahad avatar Nov 11 '18 12:11 paknahad

but in this case the field type could not determined in https://github.com/paknahad/jsonapi-bundle/blob/d09115cc0ab0d4eb08a631d7b7d759734425f615/src/Hydrator/ValidatorTrait.php#L89

waghanza avatar Nov 12 '18 10:11 waghanza

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.

paknahad avatar Nov 12 '18 14:11 paknahad

but how we can add our custom validator ?

waghanza avatar Nov 12 '18 15:11 waghanza

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 avatar Nov 13 '18 08:11 paknahad

@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 ?

waghanza avatar Nov 13 '18 09:11 waghanza

Sounds nice. but I'm so busy currently. if you could implement this feature, I would be glad to merge your PR.

paknahad avatar Nov 13 '18 10:11 paknahad