parsley.js how to trigger an error on a field with a custom error message
Asked Answered
U

5

8

It would be great if it was possible to do such thing as

$('input.specific-field').parsley('error', 'this is a custom error message');

...but I guess that isn't possible?

How could I achive such thing?

Unto answered 15/1, 2014 at 21:7 Comment(0)
F
16

Parsley has some built-in ways of adding custom error messages.

var specificField = $('input.specific-field').parsley();
# add the error
window.ParsleyUI.addError(specificField, "myCustomError", 'this is a custom error message');
# remove the error
window.ParsleyUI.removeError(specificField, "myCustomError");

More info here: http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

EDIT: This method is now deprecated (thanks to @giraff).

Finella answered 23/4, 2014 at 18:18 Comment(1)
This method is deprecated: github.com/guillaumepotier/Parsley.js/issues/1073Ballard
B
4

This is how I got it working for Parsley 2.8:

field.parsley().removeError('customValidationId');
field.parsley().addError('customValidationId', {message: "myCustomError"});

http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

Here is a turnkey-solution that I used for showing error from AJAX to fields identified by their id:

// Removing errors from previous AJAX call
if ($('.js_error').length) {
    $('.js_error').parsley().removeError('myError');
    $('.js_error').removeClass('.js_error');
}

// Showing errors from current AJAX call
for (var idField in ajaxErrors) {
    var msg = ajaxErrors[idField];
    var field = $('#field_' + idField);
    if (field.length) {
        field.addClass('js_error');
        field.parsley().removeError('myError');
        field.parsley().addError('myError', {message: msg});
    }
}

This solution will not prevent form-submit, though (because it only displays error messages in the UI, the form validation logic is not touched). That's why Parsley.js favors custom validators.

Ballard answered 22/9, 2017 at 10:55 Comment(0)
P
2

maybe this:

$('input.specific-field').parsley().UI.manageError({error: 'this is a custom error message'});
Picrotoxin answered 15/1, 2014 at 21:13 Comment(5)
or maybe $('input.specific-field').parsley().UI.addError({error: 'this is a custom error message'});, i had the same problem with displaying custom error after server respond. - I found this post : github.com/guillaumepotier/Parsley.js/issues/440Picrotoxin
EDIT seems this only works if the field has been invalidated beforeUnto
this seems to work but I'm not sure how 'correct' it is to do so: $('#register-email').parsley().UI.manageError({ error: null }); $('#register-email').parsley().UI.addError({ error: 'Emailadresse bereits vergeben' });Unto
Continuing this thread of bumping what worked, for me it was $('...').parsley().UI.manageErrorContainer().addError({err: 'Name cannot be blank'})Nostrum
With the current version of Parsley (2.8) this errors as $('...').parsley().UI is undefinedBallard
O
2
data-parsley-error-message="my message" 

worked for me see http://parsleyjs.org/doc/index.html#ui-for-javascript for more info.

Overtone answered 1/10, 2017 at 11:6 Comment(0)
P
0

Examples above appear to be for Parsley 2.0 I'm stuck with an older version but got custom errors to work with the following.

el = $('#my_input').parsley();
el.manageErrorContainer(); // set up the error list container
$(el.ulError).empty() // clear any previous errors if you want..
el.addError({error: 'Hey, mind yerself!'});
Psych answered 27/5, 2014 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.