Skip to content Skip to sidebar Skip to footer

Parsley.js - Get An Error List Using Callback

In my project I don't want to display HTML errors, but throw an alert() with list of errors on formSubmit. I guess this should be done somehow by using listeners:onFormSubmit callb

Solution 1:

As of version 2.8.0, you can access the error messages via fieldInstance.getErrorsMessages()

Example:

<formmethod="post"id="myForm"><inputtype="text"name="phone"value=""class="required"data-parsley-type="integer" /><inputtype="submit"value="Go"></form><scripttype="text/javascript">
    $(document).ready(function() {
        $("#myForm").parsley();

        window.Parsley.on('field:error', function(fieldInstance){
            // get messages & alertvar arrErrorMsg = fieldInstance.getErrorsMessages();
            var errorMsg = arrErrorMsg.join(';');
            alert(errorMsg);

            // get name of the input with erroralert(fieldInstance.$element.attr('name'));
        });
    });
</script>

Also take a look at the following question Display parsley errors in bootstrap tooltip

Solution 2:

To solved warning

parsley.min.js:16 Parsley's pubsub module is deprecated; use the 'on' and 'off' methods on parsley instances or window.Parsley

Try 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.attr('name'));
            });

Post a Comment for "Parsley.js - Get An Error List Using Callback"