Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show what validation errors Parsley.js has found?

I'm using Parsley.js to validate part of a form like in this example. However, the validate() method always returns false, even when that piece of form should validate.

No error messages are displaying, and I want to see what it is that's failed validation.

I can't see a way to get Parsley to simply return all the errors found, so I can see them in the console. Have I missed something obvious?

like image 967
Phil Gyford Avatar asked Dec 16 '14 18:12

Phil Gyford


2 Answers

You can use something like this:

window.Parsley.on('field:error', function() {
  // This global callback will be called for any field that fails validation.
  console.log('Validation failed for: ', this.$element);
});

from http://parsleyjs.org/doc/index.html#events

like image 178
Ivan Uemlianin Avatar answered Sep 30 '22 14:09

Ivan Uemlianin


There are probably (hopefully) better ways to do this, but this kind of thing is what I ended up using to get some insight into what's being validated and what is/isn't passing:

function validateAnswers(groupName) {

    var formInstance = $('.my-form').parsley();
    var isValid = formInstance.validate(groupName);

    // Just for debugging:
    $.each(formInstance.fields, function(idx, field) {
        if (typeof field.validationResult === 'boolean') {
            // Validated.
            console.log('Passed: ' + field.value);
        }  else if (field.validationResult.length > 0) {
            console.log('Failed: ' + field.validationResult[0].assert.name);
        }
    });

    return isValid;
}

This worked on my very simple form with 'required' radio buttons; no idea how it would work on larger forms with different types of fields and validation requirements.

Any better answers or improvements?

like image 31
Phil Gyford Avatar answered Sep 30 '22 15:09

Phil Gyford