Skip to content

Commit 1e89c7b

Browse files
authored
docs: Remove empty string defaults from entities (#1387)
* docs: Remove empty string defaults from entities 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. * Update index.md * Update index.md * Reinstate default empty strings where removed. * Add unvalidated title explanation * Add NotBlank constraint to title
1 parent ca88267 commit 1e89c7b

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

distribution/index.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,7 @@ Now try to add another book by issuing a `POST` request to `/books` with the fol
626626
}
627627
```
628628

629-
Oops, we forgot to add the title. Submit the request anyway, you should get a 500 error with the following message:
630-
631-
> An exception occurred while executing 'INSERT INTO book [...] VALUES [...]' with params [...]:
632-
> SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null
633-
634-
Did you notice that the error was automatically serialized in JSON-LD and respects the Hydra Core vocabulary for errors?
635-
It allows the client to easily extract useful information from the error. Anyway, it's bad to get a SQL error when submitting
636-
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).
629+
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.
637630

638631
API Platform comes with a bridge with [the Symfony Validator Component](https://symfony.com/doc/current/validation.html).
639632
Adding some of [its numerous validation constraints](https://symfony.com/doc/current/validation.html#supported-constraints)
@@ -654,6 +647,11 @@ Modify the following files as described in these patches:
654647
*/
655648
+ #[Assert\Isbn]
656649
public ?string $isbn = null;
650+
651+
* @ORM\Column
652+
*/
653+
+ #[Assert\NotBlank]
654+
public string $title = '';
657655

658656
* @ORM\Column(type="text")
659657
*/

0 commit comments

Comments
 (0)