Question

I'm new in magento. I develop one module in Magento 2.2.2.

I want to do like that in system configuration that, user can enter value between 0 to 100 in that field.

User can't enter text value and negative value in that field.

How, this field validation I can apply in system configuration.

Please help me.

System.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="erdsystem" translate="label" sortOrder="10">
            <label>ERD system</label>
        </tab>
        <section id="erdgeneral" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>General Configuration</label>
            <tab>erdsystem</tab>
            <resource>ERD_Helloworld::config_helloworld</resource>
            <group id="settings" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="width" translate="label comment" sortOrder="0" type="text" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enter Width</label>
                </field>
            </group>
        </section>
    </system>
Was it helpful?

Solution

Add this code in your system.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="erdsystem" translate="label" sortOrder="10">
            <label>ERD system</label>
        </tab>
        <section id="erdgeneral" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>General Configuration</label>
            <tab>erdsystem</tab>
            <resource>ERD_Helloworld::config_helloworld</resource>
            <group id="settings" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="width" translate="label comment" sortOrder="0" type="text" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enter Width</label>
                    <validate>required-entry validate-digits validate-not-negative-number validate-digits-range digits-range-0-100</validate>
                </field>
            </group>
        </section>
    </system>
  • required-entry : For required field
  • validate-digits : For use numbers only in this field.
  • validate-not-negative-number : For enter a number 0 or greater.
  • **validate-greater-than-zero: ** For greated than 0 [value>0]
  • validate-digits-range digits-range-0-100 : For add number between 0 to 100.

OTHER TIPS

Please try below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="erdsystem" translate="label" sortOrder="10">
        <label>ERD system</label>
    </tab>
    <section id="erdgeneral" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>General Configuration</label>
        <tab>erdsystem</tab>
        <resource>ERD_Helloworld::config_helloworld</resource>
        <group id="settings" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <field id="width" translate="label comment" sortOrder="0" type="text" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Enter Width</label>
                <validate>required-entry validate-digits validate-not-negative-number validate-digits-range digits-range-0-100</validate>
            </field>
        </group>
    </section>
</system>

For more info please read this article. https://webkul.com/blog/validation-system-configuration-magento2/

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top