Reading list Switch to dark mode

    Add Validation in System Configuration – Magento2

    Updated 21 November 2016

    Here we will see how to add validation in system configuration input box, textarea or select box.

    Validation in Configuration

    It is very simple. You just need to add “validate” tag for field in system.xml file and pass validation class between validate tag.
    Here is example of validation for email.

    <?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="webkul" translate="label" sortOrder="10">
                <label>Webkul</label>
            </tab>
            <section id="test" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Test Configuration</label>
                <tab>webkul</tab>
                <resource>Webkul_Test::config_test</resource>
                <group id="settings" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <field id="custom_email" translate="label comment" sortOrder="0" type="text" showInDefault="1" showInWebsite="0" showInStore="0">
                        <label>Enter Email Id</label>
                        <validate>validate-email</validate>
                    </field>
                </group>
            </section>
        </system>
    </config>

    Validation

    You can also pass multiple validation classes in validation tag.Add space between classes.

    <validate>required-entry validate-email</validate>

    Some Important validation classes are

    Searching for an experienced
    Magento Company ?
    Find out More
    'validate-no-html-tags'         => 'HTML tags are not allowed'
    'validate-select'               => 'Please select an option.'
    'required-entry'                => 'This is a required field.'
    'validate-number'               => 'Please enter a valid number in this field.'
    'validate-digits'               => 'Please use numbers only in this field. Please avoid spaces or other characters such as dots or commas.'
    'validate-date'                 => 'Please enter a valid date.'
    'validate-email'                => 'Please enter a valid email address. For example [email protected].'
    'validate-url'                  => 'Please enter a valid URL. Protocol is required (http://, https:// or ftp://)'
    'validate-not-negative-number'  => 'Please enter a number 0 or greater in this field.'
    'validate-zero-or-greater'      => 'Please enter a number 0 or greater in this field.'
    'validate-state'                => 'Please select State/Province.'
    'validate-cc-number'            => 'Please enter a valid credit card number.'
    'validate-data'                 => 'Please use only letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field, first character should be a letter.'

    That’s all for validation in system configuration in Magento2.

    If you have any query, comment below.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    1 comments

  • Vipul Kumar
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home