简体   繁体   English

欧芹阻止提交输入验证

[英]Parsley prevent submit on input validation

I am trying to use parsley to validate a varying number of inputs (order line items) encased within a DIV structure (the main form is for order details). 我正在尝试使用parsley来验证包含在DIV结构中的不同数量的输入(订单行项目)(主要表单用于订单详细信息)。

Due to this I am converting the data outside the form into JSON and injecting into a hidden field.. All works great (including the server side validation). 由于这个原因,我将表单外的数据转换为JSON并注入隐藏字段。所有工作都很棒(包括服务器端验证)。

The issue I am having is that on form submit, the validation runs through, and shows the errors as required, but the form continues through the submission process on 'fail'. 我遇到的问题是,在表单提交时,验证会一直运行,并根据需要显示错误,但表单会在“失败”的提交过程中继续。

is there any way to see if there are any errors present on the page and prevent the page submit to continue? 是否有任何方法可以查看页面上是否存在任何错误并阻止页面提交继续?

Here is what is not working: 这是不起作用的:

$('#submit-form').on('click', function(e){
    e.preventDefault();

    // Validate line items
    $.each($('#lineItems').find('.input-group input'), function(i, val){
        $(val).parsley(parsleyConfig).validate();
    });

All the errors show up but the page POSTs still. 所有错误都显示但页面POST仍然存在。 However, if the data in the main form is no good, on submit it will pop up the validation errors, that is set by this: 但是,如果主窗体中的数据不好,提交时会弹出验证错误,由以下设置:

// Validation
var parsleyConfig = {
    errorsContainer: function(pEle) {
        return pEle.$element.parent().siblings('.text-danger');
    }
};
$('#editForm').parsley(parsleyConfig);

I have tried using the 'form:submit' event with parsley to no luck either... 我尝试过使用欧芹的'form:submit'活动也没有运气......

    $('#editForm').parsley(parsleyConfig).on('form:submit', function(){
    // Validate line items
    $.each($('#lineItems').find('.input-group input'), function(i, val){
        $(val).parsley(parsleyConfig).validate();
    });
});

I cannot seem to find a way to get the returned value of if the validation has passed and abort my submit script... 如果验证已通过并且中止我的提交脚本,我似乎无法找到获取返回值的方法...

inside your 'form:submit' event listener write this code: 在你的'form:submit'事件监听器中写下这段代码:

//the validate() calls the error validation and add the error messages:
if ($('#editForm').parsley().validate() == false) {
    //Do nothing:
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM