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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions distribution/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,7 @@ Now try to add another book by issuing a `POST` request to `/books` with the fol
}
```

Oops, we forgot to add the title. Submit the request anyway, you should get a 500 error with the following message:

> An exception occurred while executing 'INSERT INTO book [...] VALUES [...]' with params [...]:
> SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null

Did you notice that the error was automatically serialized in JSON-LD and respects the Hydra Core vocabulary for errors?
It allows the client to easily extract useful information from the error. Anyway, it's bad to get a SQL error when submitting
a request. It means that we didn't use a valid input, and [it's a bad and dangerous practice](https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html).
The book is successfully created but there is a problem; we did not give it a title. It makes no sense to create a book record without a title so we really should have some validation measures in place to prevent this from being possible.

API Platform comes with a bridge with [the Symfony Validator Component](https://symfony.com/doc/current/validation.html).
Adding some of [its numerous validation constraints](https://symfony.com/doc/current/validation.html#supported-constraints)
Expand All @@ -654,6 +647,11 @@ Modify the following files as described in these patches:
*/
+ #[Assert\Isbn]
public ?string $isbn = null;

* @ORM\Column
*/
+ #[Assert\NotBlank]
public string $title = '';

* @ORM\Column(type="text")
*/
Expand Down