Api Platform conference
Register now
v2.6 Validation
API Platform Conference
September 19-20, 2024 | Lille & online

The international conference on the API Platform Framework

API Platform Conference 2024: meet the best PHP, JavaScript and API experts

Learn more about the event, register for the conference, and get ready for two days of inspiration, ideas, and knowledge-sharing with our incredible lineup of renowned specialists and advocates.

Register now

Validation

API Platform Admin manages automatically two types of validation: client-side validation and server-side (or submission) validation.

Client-side Validation

If the API documentation indicates that a field is mandatory, API Platform Admin will automatically add a required client-side validation.

For instance, with API Platform Core as backend, if you write the following:

<?php
// api/src/Entity/Book.php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource]
class Book
{
    #[Assert\NotBlank]
    public ?string $title = null;
}

If you create a new book and touch the “Title” field without typing, you will see:

Required title field

Server-side Validation

When the form is submitted and if submission errors are received, API Platform Admin will automatically show the errors for the corresponding fields.

To do so, it uses the submission validation feature of React Admin, and the mapping between the response and the fields is done by the schema analyzer with its method getSubmissionErrors.

API Platform Core is supported by default, but if you use another backend, you will need to override the getSubmissionErrors method.

For example if you have this code:

<?php
// api/src/Entity/Book.php

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

#[ApiResource]
class Book
{
    #[Assert\Isbn]
    public ?string $isbn = null;
}

If you submit the form with an invalid ISBN, you will see:

Submission error field

You can also help us improve the documentation of this page.

Made with love by

Les-Tilleuls.coop can help you design and develop your APIs and web projects, and train your teams in API Platform, Symfony, Next.js, Kubernetes and a wide range of other technologies.

Learn more

Copyright © 2023 Kévin Dunglas

Sponsored by Les-Tilleuls.coop