10

I developed an extension for Magento2 that saves configs using a system.xml and I would like to validate the values the user inputs. How can i validate those values?

2
  • What type of values you want to validate, if the values are countable, try to use drop down and restrict user. Commented Dec 17, 2015 at 10:42
  • @amit_game unfortunately it's input fields for text and i need some custom validation ( at least that the input is not Null) Commented Dec 17, 2015 at 10:50

4 Answers 4

14

You can apply validation on textbox in magento 2 like this,

<field id="custom_path" translate="label comment" type="text" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0">
   <label>Custom Admin Path</label>
   <validate>required-entry</validate>              
   <comment>You will have to sign in after you save your custom admin path.</comment>
</field>

You can keep validate-alphanum or validate-number for alphanumeric and numeric value inside validate tag.

Thanks.

8

you can attach a backend model to the field and you can validate the value server side.

You can find an example in Magento_Backend/etc/adminhtml/system.xml:

<field id="base_url" translate="label comment" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Base URL</label>
    <backend_model>Magento\Config\Model\Config\Backend\Baseurl</backend_model>
    <comment>Specify URL or {{base_url}} placeholder.</comment>
</field>

as you can see, this field has the backend model Magento\Config\Model\Config\Backend\Baseurl.
This means that the method Magento\Config\Model\Config\Backend\Baseurl::beforeSave will be called whe saving the field. You can use this method to throw exceptions if something is wrong and the config won't be saved.

You can create your own model that acts as a backend model for your field.

5

try this

 <field id="test" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Handling Fee</label>
                    <validate>required-entry</validate>
                </field>

see below picture it's validated when I'm hit the save button.

enter preformatted text here

1
  • how to add asterisk mark (*) after label? Commented Apr 23, 2019 at 12:23
0

Below is an example of Validating for required-entry in system.xml, hope it will help you -

<a2bizz_field_test>
    <label>Text Value</label>
    <frontend_type>text</frontend_type>
    <validate>required-entry</validate>
    <sort_order>5</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</a2bizz_field_test>

for custom validation go through the below URL -

http://alanstorm.com/magento_system_config_validation

1
  • your answer is for magento 1. The op asked about magento 2 Commented Dec 17, 2015 at 12:13

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.