If you use Symfony's Doctrine UniqueEntity constraint, import the default
bundle route as shown in the README.
To customize server-side uniqueness validation, create your own controller and point the bundle config to your route:
# config/packages/fp_js_form_validator.yaml
fp_js_form_validator:
routing:
check_unique_entity: app_custom_unique_controller# config/routes/fp_js_form_validator_custom.yaml
app_custom_unique_controller:
path: /custom_unique_controller
controller: App\Controller\CustomUniqueController::index<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
final class CustomUniqueController
{
public function index(Request $request): JsonResponse
{
$data = $request->request->all();
// Add your uniqueness lookup here.
$isUnique = true;
return new JsonResponse($isUnique);
}
}The request data has this shape:
$data = [
'message' => 'This value is already used.',
'service' => 'doctrine.orm.validator.unique',
'repositoryMethod' => 'findBy',
'fields' => ['email'],
'ignoreNull' => '1',
'groups' => ['Default', 'User'],
'entityName' => 'App\Entity\User',
'entityId' => 15,
'data' => [
'email' => 'john_doe@example.com',
],
];When the form is bound to an object that has a getId() method, entityId
contains the current object id. Custom uniqueness controllers can use it to
exclude the edited entity from their lookup.
Return true when the value is unique and false when it is already used.