1
var dataForm = jQuery('#form');
dataForm.mage('validation');
function submitForm(){                
    if (dataForm.valid()){
        dataForm.submit();
    }
}

Everything is working well what i want some kind of callback function when valid() returns false.

I tried and found that there is some option for adding custom error messages like

dataForm.mage('validation',{
    errorPlacement:function(error,element){
        var errorPlacement = element;
        errorPlacement.after(error);
        /* we can do custom stuff here */
    }
});

But this doesn't works well it only create element for first time when validation called.

I need callback function that call everytime whenever validation failed.

1
var dataForm = jQuery('#form');
dataForm.mage('validation');
function submitForm(){                
    if (dataForm.valid()){
        dataForm.submit();
    }else{
        var elements = $('#submitbtn');
        $('.magerror').remove();
        var error = "<div class='magerror'>This is required field</div>";
        elements.after(error);
    }
}

<form class="forms" id="form">
    <div class="aaaa">
        <button type="submit" id="submitbtn">Submit</button>
    </div>
</form>
  • you can try with above code – Rakesh Jesadiya Oct 18 '16 at 9:45
  • Thanks but this solution will not solve my problem. what i want is a callback for valid() method which triggers every time when the valid() returns false – Amit Singh Oct 18 '16 at 9:54
  • you have to pass in else condition with function name and add else code in those functions – Rakesh Jesadiya Oct 18 '16 at 9:55
1

I am not able to find the correct callback function but this workaround worked for me.

var dataForm = jQuery('#form');
dataForm.mage('validation');
function submitForm(){                
    if (dataForm.valid()){
        dataForm.submit();
    }
    else{
        jQuery('input.mage-error').each(function(){
            /* Do our custom stuff here */
        });
    }
}

What this does is whenever valid() returns false Magento adds mage-error class to each element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.