0

I wish to place the error after the parent of the element being validated. I tried to change the place via error placement function like below

<script>
    require([
        "jquery",
        "mage/mage"
    ], function($){
        var dataForm = $('#reward-points-form');
        dataForm.mage('validation', {
            errorPlacement: function(error, element) {
                if (element.hasClass('input-reward')) {
                    error.appendTo('.input-error');
                }
                else {
                    error.insertAfter(element);
                }
            }
        });

    });
</script>

However, the script is not working as it's written in the Knockout HTML file. Please advise.

0

I managed to add errorPlacement code in the JS file where validation function is called.

Before

validate: function() {
   return $(form).validation() && $(form).validation('isValid');
},

After

validate: function() {
    return $(form).validation({errorPlacement: function(error, element) {
                    error.appendTo('.input-error');
    }}) && $(form).validation('isValid');
},

This validate function was called on form submit action.

I wrote mixin for this change in my custom theme

define(
[
    'jquery'
],
function ($) {
    "use strict";
    var form = '#reward-points-form';
    return function (validateRewardPoints) {
        return validateRewardPoints.extend({
            validate: function() {
                return $(form).validation({errorPlacement: function(error, element) {
                    error.appendTo('.input-error');
                }}) && $(form).validation('isValid');
            },
        });
    }
});

and called mixin in requirejs

var config = {
config: {
    mixins: {
        'Mirasvit_Rewards/js/checkout/cart/rewards_points': {
            'Mirasvit_Rewards/js/checkout/cart/rewards_points_mixin': true
        }
    }
}};
| improve this answer | |

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.