Validation

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

API Platform Admin は、クライアント側の検証とサーバー側 (または送信) の検証の 2 種類の検証を自動的に管理します。

Client-side Validation

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

フィールドが必須であることが API ドキュメントに示されている場合、API プラットフォーム管理者は必要なクライアント側の検証を自動的に追加します。

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

たとえば、バックエンドとして API プラットフォームを使用する場合、次のように記述します。

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\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.

フォームが送信され、送信エラーが受信された場合、API プラットフォーム管理者は対応するフィールドのエラーを自動的に表示します。

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.

そのために、React Admin の送信検証機能を使用し、応答とフィールド間のマッピングは、メソッド getSubmissionErrors を使用してスキーマ アナライザーによって行われます。

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

API プラットフォームはデフォルトでサポートされていますが、別のバックエンドを使用する場合は、getSubmissionErrors メソッドをオーバーライドする必要があります。

For example if you have this code:

たとえば、次のコードがある場合:

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\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:

無効な ISBN コードでフォームを送信すると、次のように表示されます。

Submission error field