3

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>
3

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-digits-range digits-range-0-100 : For add number between 0 to 100.
  • 1
    Ah..!! Thanks Man :) It's perfect As I want. – Michael Jones Jul 20 '18 at 18:08
  • Welcome !! Happy Coding :) – Rohan Hapani Jul 20 '18 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, privacy policy and cookie policy

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