For a callback defined on the entity:
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
#[Assert\Callback('checkEmail')]
class User
{
public function checkEmail(ExecutionContextInterface $context): void
{
if (...) {
$context
->buildViolation('Email is not valid')
->atPath('email')
->addViolation()
;
}
}
}or a callback placed on the method:
#[Assert\Callback]
public function checkEmail(ExecutionContextInterface $context): void
{
if (...) {
$context
->buildViolation('Email is not valid')
->atPath('email')
->addViolation()
;
}
}provide the matching JavaScript callback. Pass a stable sourceId when you
display errors manually:
$('form#user').jsFormValidator({
callbacks: {
checkEmail: function() {
var errors = [];
if (...) {
errors.push('Email is not valid');
}
$('#user_email').jsFormValidator('showErrors', {
errors: errors,
sourceId: 'check-email-callback'
});
}
}
});Pure JavaScript:
var field = document.getElementById('user');
FpJsFormValidator.customize(field, {
callbacks: {
checkEmail: function() {
var errors = [];
if (...) {
errors.push('Email is not valid');
}
var email = document.getElementById('user_email');
FpJsFormValidator.customize(email, 'showErrors', {
errors: errors,
sourceId: 'check-email-callback'
});
}
}
});For an external callback:
<?php
namespace App\Entity;
use App\Validator\ExternalValidator;
use Symfony\Component\Validator\Constraints as Assert;
#[Assert\Callback([ExternalValidator::class, 'checkEmail'])]
class User
{
// ...
}define the same class and method path in JavaScript:
$('form#user').jsFormValidator({
callbacks: {
'App\\Validator\\ExternalValidator': {
checkEmail: function () {
// ...
}
}
}
});You can also define it without nesting, as in 3.5.1, but only if the method name is unique:
$('form#user').jsFormValidator({
callbacks: {
checkEmail: function () {
// ...
}
}
});