4

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>

2 Answers 2

7

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.
2
  • 1
    Ah..!! Thanks Man :) It's perfect As I want. Jul 20, 2018 at 18:08
  • Welcome !! Happy Coding :) Jul 20, 2018 at 18:16
0

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/

Your Answer

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

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