Skip to content

Latest commit

 

History

History
executable file
·
42 lines (38 loc) · 1.55 KB

File metadata and controls

executable file
·
42 lines (38 loc) · 1.55 KB

3.2 Error display

The showErrors function should delete previous errors and display new ones. The example below shows you how it works in our default implementation. The sourceId variable is an identifier of validation source. It can be used to prevent any confusion between the field's errors and other errors which have come from other sources. For example, this field (user_email) may contain the Email constraint, and its own parent may contain the UniqueEntity constraint by this field. Both of these errors should be displayed for the email field, but the first one will be displayed/deleted by the 'user_email' validator and the second one - by the parent. By default we use this variable to add it as a class name to 'li' tags, and then we use it to remove the errors by this class name:

$('#user_email').jsFormValidator({
    showErrors: function(errors, sourceId) {
        var list = $(this).prev('ul.form-errors');
        if (!list.length) {
            list = $('<ul class="form-errors"></ul>');
            $(this).before(list);
        }
        list.find('.' + sourceId).remove();

        for (var i in errors) {
            var li = $('<li></li>', {
                'class': sourceId,
                'text': 'custom_'+ errors[i]
            });
            list.append(li);
        }
    }
});

Pure Javascript:

var field = document.getElementById('user_email');
FpJsFormValidator.customize(field, {
    showErrors: function(errors, sourceId) {
        for (var i in errors) {
            // do something with each error
        }
    }
});