0

On my magento 2.4.4 I'm creating a new module with its configuration in system.xml. The file itself works fine, I've created a yes/no field to enable/disable my feature plus an input text to add some data:

<field id="enabled" translate="label comment" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Enabled</label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="customfield" translate="label comment" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Custom Field</label>
</field>

My issue is that my "custom field" should have a price inside, so I'd like to have the field not as a simple text but as a proper field price (like the one in the product form). I've looked on the internet but I cannot find what type to use instead of "type to order to have a price input field.

Does such a type even exist? How can I edit my field to have a proper price field?

Thanks

1

1 Answer 1

1

In System.xml, when you need to add a restriction on a specific field, you can achieve this by adding embedded validators within your configuration file. There exists a variety of validators, including:

  1. validate-select
  2. required-entry
  3. validate-number
  4. validate-digits
  5. validate-not-negative-number and several others.

In the context of your specific requirement, you should utilize the validate-number validator to enforce numeric value constraints.

Your code should be:

<field id="enabled" translate="label comment" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Enabled</label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="customfield" translate="label comment" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Custom Field</label>
    <validate>validate-number</validate>
</field>

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.