Parsley custom validator is not working for javascript installation

Petr Felzmann picture Petr Felzmann · Jun 16, 2014 · Viewed 15.1k times · Source

I have the simplest possible form with the simplest custom validator and it is not working, see http://jsfiddle.net/M55M4/ What is wrong?

<form id="myform">
    <input type="text" data-parsley-myvalidator="foo" data-parsley-required />
    <input type="submit" />
</form>
$('#myform').parsley({
    validators: {
        myvalidator: function () {
            return {
                validate: function (value, requirements) {
                    alert('myvalidator');
                    return false;
                },
                priority: 32
            }
        }
    },
    messages: {
        myvalidator: 'my validator failed'
    }
});

Answer

Michael Potter picture Michael Potter · Jun 23, 2014

If Parsley js version is not critical I recommend you to update it to the latest one - v.2.0.2. According to the new API, custom validator should be defined the following way (http://jsfiddle.net/M55M4/6/):

window.ParsleyValidator.addValidator('myvalidator', 
function (value, requirement) {
    alert('myvalidator');
    return false;
}, 32)
.addMessage('en', 'myvalidator', 'my validator failed');