Skip to main content
3 of 3
deleted 35 characters in body; edited tags
R T
  • 1.3k
  • 1
  • 20
  • 27

how to add custom validation to product options field?

I'm trying to add custom validation to the product option, I'm following the [tutorial][1] and here it says to add custom validation element to the attribute data-validate like data-validate="{required:true, 'validate-custom-name':true}"

so far I've tried this:

require(['jquery'], function ($) {
                    
    var rowId = $('#options_<?php /* @escapeNotVerified */ echo $rowOpId ?>_text');

    $(document).ready(function(){
                    
        var data = $(rowId).data("validate"); // default object value
                   
        if (!("validate-custom-name" in data)) {
            $(rowId).extend($(rowId).data("validate"), {"validate-custom-name": true});
        }
    });
});

nothing happened so far. it only shows:

<input type="text" value="" name="options[1]" data-validate="{required:true}" class="input-text product-custom-option" id="options_1_text" aria-required="true">

this is the source code of the html element, here you can see that data-validate option is not being updated. [1]: http://www.webmull.com/magento-2-add-new-custom-form-validation/

R T
  • 1.3k
  • 1
  • 20
  • 27