magento2:magento2中的验证工作方式


12

在magento1中,我们可以像这样检查验证:

var formToValidate = $('form-validate');
    var validator = new Validation(formToValidate);
    if(validator.validate()) {
        // Here we can code after validation.
    }

但是在magento2中,我想检查所有验证,但不想在成功后提交表单。那我该如何用magento2形式演奏呢?

Answers:


19

尝试这个:

require(["jquery"], function ($) {
    $(document).ready(function () {
        $('#my-button-name').click(function () { // The button type should be "button" and not submit
            if ($('#form-name').valid()) {
                alert("Validation pass");
                return false;
            }
        });
    });
});

希望这可以帮助!


1
就像魅力一样!!
纳林·萨瓦利亚

0

您可以在具有data-mage-init价款的地方使用解决方案

  require([
        'jquery',
        'jquery/ui'
    ], function($){
        $(".MYbuttonClass").on("click", function() {

           if ( $('.form-Class').validation() && $('.form-cart').validation('isValid') === true) {
                //Submit form    
                $('.form-Class').submit();

            }

        });

    });
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.