Skip to content

Latest commit

 

History

History
executable file
·
57 lines (50 loc) · 1.1 KB

File metadata and controls

executable file
·
57 lines (50 loc) · 1.1 KB

3.6 The Choice constraint. How to get the choices list from a callback

In general, it works in the same way as the previous step. In case if you have:

namespace Acme\DemoBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @Assert\Choice(callback = {"Acme\DemoBundle\Entity\Util", "getGenders"})
     */
    protected $gender;
}
namespace Acme\DemoBundle\Entity;

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

Then:

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

Pure Javascript:

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

also, you can simplify it as it was described here