core icon indicating copy to clipboard operation
core copied to clipboard

How to validate entity's proprty with enum type

Open alebedev80 opened this issue 2 years ago • 7 comments

There is an entity with property $language:

private Language $language = Language::ES;

On post request data with language from enum list (e.g. es) all is ok. But if value out of list response error (e.g. 00):

{
    "title": "An error occurred",
    "detail": "The data must belong to a backed enumeration of type App\\Core\\Language",
    "status": 400,
    "type": "/errors/400"
}

Choice and custom validator useless because they execute AFTER deserialization where that error appears. So i need proper 422 error response. What and how to fix?

alebedev80 avatar Nov 22 '23 14:11 alebedev80

You can use a less strictly typed input, and validate it before hydrating your real object in a custom processor. Would be great if Api platform could natively deal with it !

bertrandseurot avatar Nov 22 '23 16:11 bertrandseurot

$language is a parameter of your object constructor, right ?

bertrandseurot avatar Nov 22 '23 17:11 bertrandseurot

$language is a parameter of your object constructor, right ?

@bertrandseurot no. simple doctrine's entity.

alebedev80 avatar Nov 22 '23 18:11 alebedev80

Please help somebody

alebedev80 avatar Dec 19 '23 06:12 alebedev80

disable_type_enforcement ?

soyuka avatar Dec 19 '23 10:12 soyuka

@soyuka what's it? i couldn't find documentation about it(

alebedev80 avatar Dec 19 '23 12:12 alebedev80

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 17 '24 14:02 stale[bot]