0

It is possible to pass value from XML to JS components.

 <field name="postcode" component="Magento_Ui/js/form/element/post-code" formElement="input" sortOrder="70">
            <settings>
                <dataType>text</dataType>
                <visible>true</visible>
                <label translate="true">Zip/Postal Code</label>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>

For example here we can pass the label. In our case Zip/Postal Code.

I wish to pass a custom field value

For e.g.

...
<settings>
    .....
   <custom_field>Custom Value</custom_field>
   ...
</settings>
...

Also tried by passing it as an attribute for the field node as such

<field custom_field="Custom Value" ....

with no luck. The value is not available in the JS component (in this example Magento_Ui/js/form/element/post-code)

Any ideas on how to achieve this?

1

Try this:

<field name="postcode" component="Magento_Ui/js/form/element/post-code" formElement="input" sortOrder="70">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="custom_field" xsi:type="string">Custom Value</item>
            <!-- here you can add anything you need without restrictions -->
        </item>
    </argument>
    ....
</field>

but you also might need to create your own component in order to be able to use the custom value, or extend the original component.

3
  • This indeed works. But it duplicates the value for all fields that use that component (thus making redundant the whole thing in my case). If we have two postcode fields on the same page that use the same component (shipping and billing) both will have the same value (the one that loads last). Any other ideas? Jan 28 '20 at 11:49
  • not sure I understand. Each component should be a separate instance. if you send different custom field values they should be handled separately.
    – Marius
    Jan 28 '20 at 13:43
  • It's treated like a singleton. The last configured value applies. Jan 29 '20 at 6:10

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.