Parsley.JS - Get an error list using callback

。_饼干妹妹 提交于 2019-12-12 14:43:10

问题


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 callback, however none of passed variables does not contain "clean" version of errors. Parsley documentation also lacks of this feature.


回答1:


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

Example:

<form method="post" id="myForm">
    <input type="text" name="phone" value="" class="required" data-parsley-type="integer" />
    <input type="submit" value="Go">
</form>

<script type="text/javascript">
    $(document).ready(function() {
        $("#myForm").parsley();

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

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

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




回答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'));
            });


来源:https://stackoverflow.com/questions/18120734/parsley-js-get-an-error-list-using-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!