Skip to content

docs: Remove empty string defaults from entities #1387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 7, 2021

Conversation

GaryClarke
Copy link
Contributor

I'm not getting the SQL exception in 'Validating Data' section; empty strings are being stored instead of null being attempted.

The simplest fix is to remove the default empty strings from the entities.

This is using the Symfony and Composer install.

I'm not getting the SQL exception in 'Validating Data' section; empty strings are being stored instead of null being attempted.

The simplest fix is to remove the default empty strings from the entities.

This is using the Symfony and Composer install.
@alanpoulain
Copy link
Member

I think it would be better to change only the title field by:

public ?string $title = null;

@GaryClarke
Copy link
Contributor Author

@alanpoulain works for me...I'll update with this

@dunglas
Copy link
Member

dunglas commented Jun 23, 2021

Allowing null values here is bad DB and entity design IMHO. I would prefer to just change the wording to tell that allowing the user to not fill the title is not what we except, and introduce validation with the #[NotBlank] assertion. WDYT?

@alanpoulain
Copy link
Member

Not sure what you mean Kevin. The resource is not linked yet to DB. The Doctrine attribute can be not nullable but this one needs to be nullable in order to deserialize it correctly and have a NotBlank validation on it working as expected.

@GaryClarke
Copy link
Contributor Author

I see what Kevin is saying. I you were just designing the table (and not thinking about API's or anything else), you wouldn't allow book.title to be null....it's sort of sacrificing good design just to demonstrate how something else works.

@dunglas
Copy link
Member

dunglas commented Jun 23, 2021

Exactly!

@alanpoulain
Copy link
Member

But currently (and sadly?) there is no other way to correctly have validation without using for instance DTOs.

@dunglas
Copy link
Member

dunglas commented Jun 23, 2021

I'm not sure to understand @alanpoulain

@alanpoulain
Copy link
Member

My bad maybe it works since null is not given directly in the input body.
You can leave it like this then but you need to modify the following paragraphs (when linking to Doctrine) @GaryClarke.

@GaryClarke
Copy link
Contributor Author

@alanpoulain good point...done now 👍

@dunglas
Copy link
Member

dunglas commented Jun 23, 2021

I don't think that we must remove the default values (this could trigger "initialized value" exceptions. Couldn't we remove the part about the SQL error and say that sending no values or an empty string doesn't trigger an error while it should?

@GaryClarke
Copy link
Contributor Author

OK...I'll change to that.

@dunglas dunglas merged commit 1e89c7b into api-platform:2.6 Sep 7, 2021
@dunglas
Copy link
Member

dunglas commented Sep 7, 2021

Thank you very much @GaryClarke, and sorry for the delay.

@GaryClarke
Copy link
Contributor Author

You're welcome @dunglas 🤝 ..even though I'd totally forgot about this 😂

@GaryClarke GaryClarke deleted the patch-2 branch October 13, 2021 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants