0

Validation field with admin Yes/No config value, so that am adding helper file to get config enable value to validate the field. So i want to call this value with if condition in html ko file.

     <div class="control">
                <input
                    id="vat_id"
                    name="vat_id"
                    type="text"
                    data-bind="attr : { placeholder: $t(getPlaceholder('vat_id')) }, textInput : address.vatId"
                    data-validate="{ required : true }" // here i need if condition like <?php if($val)==1 { ?> data-validate="{ required : true }" <?php }else { ?> data-validate="" <?php } ?> 
                    class="required validate-vat-number"/>
            </div>

My helper file code

    public function ValidationEnable() {
    return $this->scopeConfig->getValue('step/return/validatation_activate', ScopeInterface::SCOPE_STORE);
     }
           

I want to add this helper function in html ko file. In phtml I know, how to do it in html ko file

3 Answers 3

0

Below is an example of If condition: try it

<!-- ko if: myFlag -->
...some stuff for the true case
<!-- /ko -->
<!-- ko ifnot: myFlag -->
...some stuff for the else (false) case
<!-- /ko -->

For more information, refer here

1
  • I updated my question can u plz add with my stuff
    – User0434
    Commented Oct 20, 2021 at 5:13
2

You can call the output of the function ValidationEnable by getting the return from the PHTML file to the JS file via using configs, and from there, you can assign object's properties in JS file or by setting it in a KO object and using that in HTML file

for ref = Block file or ViewModel -> PHTML -> JS File -> HTMl file

in the PHTML file, you can use this to send the data to JS file, please refer to the code below - focusing on the line $block->getJsonConfig() -

<script type="text/x-magento-init">
{
    "[data-role=priceBox][data-price-box=product-id-<?= $block->escapeHtml($_product->getId()) ?>]": {
        "priceBox": {
            "priceConfig":  <?= /* @noEscape */ $block->getJsonConfig() ?>
        }
    }
}
1

You can't directly do this as html files are rendered in the browser meaning you can't access PHP.

The most common way to do this is to add the value to window.checkoutConfig in PHP then take the value from there doing something like this:

<!-- ko if: window.checkoutConfig.ValidationEnable -->

<!-- /ko -->

Or for an attribute like you requested:

data-bind="attr: {required: window.checkoutConfig.ValidationEnable}"
7
  • 1
    Thanks for reply can I add like this <!-- ko if: window.checkoutConfig.ValidationEnable==1 --> <div class="control"> <input id="vat_id" name="vat_id" type="text" data-bind="attr : { placeholder: $t(getPlaceholder('vat_id')) }, textInput : address.vatId" data-validate="{ required : true }" class="required validate-vat-number"/> </div> <!-- /ko -->
    – User0434
    Commented Oct 20, 2021 at 9:31
  • Yeah. That will render the input if the value is 1
    – Ben Crook
    Commented Oct 20, 2021 at 10:10
  • but how can they know that helper file code is calling like we are using phtml $val=$this->helper('test\hello\Helper\Data');echo $val->ValidationEnable(); How to declare helper function in html plz provide syntax with full code
    – User0434
    Commented Oct 20, 2021 at 11:06
  • You need to add that to window.checkoutConfig in your PHP. HTML files cannot access PHP values.
    – Ben Crook
    Commented Oct 20, 2021 at 11:17
  • how to add. plz show syntax
    – User0434
    Commented Oct 20, 2021 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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