Pennywise83
Pennywise83

Reputation: 1784

Force an error on a field with Parsley.js

I'm using parsley (1.x) for the client-side validation of a form. I need to force an error on a certain field after the server-side validation.

<form>
<input type="text" name="field1" >
<input type="text" name="field2" >
<input type="submit" value="Send">
</form>

I've tried the methods described in this page but it doesn't work: https://github.com/guillaumepotier/Parsley.js/issues/111

Anyone can help me?

Thx

Upvotes: 1

Views: 3927

Answers (2)

Fehim
Fehim

Reputation: 51

In version 2.4.3 you can do this:

$('#fieldName').parsley().addError('forcederror', {message: 'My error message.', updateClass: true});

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

Upvotes: 5

Pennywise83
Pennywise83

Reputation: 1784

I've managed to resolve this issue writing a new validation rule that return alwais an invalid field.

forcevalidation : function() {
                    return {
                        validate : function(val, elem, self) {
                            return false;
                        },
                        priority : 32
                    };
                },

When I receive the ajax response I add the validator to the affected field and then I remove it:

                affectedInput.attr('parsley-forcevalidation', true);
                affectedInput.attr('parsley-error-message', errorMessage);
                affectedInput.parsley('destroy');
                affectedInput.parsley('validate');
                affectedInput.removeAttr('parsley-forcevalidation');

Upvotes: 2

Related Questions