Parsley Form Validation - Event Listeners

丶灬走出姿态 提交于 2019-12-04 13:24:36

You are doing what you shouldn't do. Looking at the Parsley docs one can see warning like this:

you must remove data-validate="parsley" auto-binding code in your forms DOM to allow you to override the default processing and use Parsley purely from javascript.

After that you can do $('.test').parsley(...).

Also note that you are hanging listeners incorrectly. The right way is to put them inside listenter: {} property just like this:

$('.test').parsley({
    listeners: {
        onFieldValidate: function(elem, ParsleyField) {
            console.log("validate", elem);
            return false;
        },
        onFieldError: function(elem, constraints, ParsleyField) {
            console.log("error", elem);
        }
    }
});

Simple example could be found here

Waseem Safder

You can simple check if your form is validated or not by using following code.

$( '#formId' ).parsley( 'isValid' );

I ended up using this in conjunction with bootstrap tooltips, the below snippet adds a title to invalid fields.

parsleyContainer = Field container, this could be a <form> tag for example

var parsleyContainer = $(this).closest('.tab-pane');
$(parsleyContainer).attr('data-parsley-validate', 'true');
$(parsleyContainer).parsley({
    errorsContainer: function (ParsleyField) {
        return ParsleyField.$element.attr("title");
    },
    errorsWrapper: false,
});

if($(parsleyContainer).parsley().validate()) {
    //do something is validated
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!