Skip to content

Latest commit

 

History

History
executable file
·
63 lines (50 loc) · 1.08 KB

3_6.md

File metadata and controls

executable file
·
63 lines (50 loc) · 1.08 KB

3.6 The Choice constraint callback

If a Choice constraint receives choices from a PHP callback:

<?php

namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    #[Assert\Choice(callback: [Util::class, 'getGenders'])]
    private ?string $gender = null;
}
<?php

namespace App\Entity;

class Util
{
    public static function getGenders(): array
    {
        return ['male', 'female'];
    }
}

provide the same callback on the JavaScript side:

$('form#user').jsFormValidator({
    callbacks: {
        'App\\Entity\\Util': {
            getGenders: function() {
                return ['male', 'female'];
            }
        }
    }
});

Pure JavaScript:

var field = document.getElementById('user');
FpJsFormValidator.customize(field, {
    callbacks: {
        'App\\Entity\\Util': {
            getGenders: function() {
                return ['male', 'female'];
            }
        }
    }
});

You can also use the simplified callback form described in 3.5.2 when the method name is unique.