简体   繁体   中英

Parsley.js issues, adding class to parent of form field

I've seen this question a few times, and from what I can gather, either of these two options should work. However I cannot get errorsContainer to work. My goal is to add the error and valid classes to the direct parent of the form field. Extra kudos if if you can tell me how to disable the appending of the errors list while still adding these validation classes.

var parsleyConfig = {
    errorClass: 'error',
    successClass: 'valid',
    errors: {
        errorsContainer: function(el) {
            return el.$element.parent();
            //return $(el).closest('.parent');
        }
    }
}

var parsleyConfig = {
    errorClass: 'error',
    successClass: 'valid',
    errorsContainer: function(el) {
        return el.$element.parent();
        //return $(el).closest('.parent');
    }
}

$('#registerForm').parsley(parsleyConfig);

In both cases, the class is added to the form field itself, not its parent. Result is the same with data-parsley-validate included or not in the form element. Also running the latest 2.0 thanks!!

You have to use the classHandler option instead of errorsContainer

var parsleyOptions = {
  errorClass: 'error',
  successClass: 'valid',
  errorsMessagesDisabled: true,
  classHandler: function(el) {
    return el.$element.parent();
  }
};
$formSelector.parsley(parsleyOptions);

errorsContainer return the $element where errors messages will be appended as described in annotated source defaults options .

errorsMessagesDisabled prevent Parsley from adding errors list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM