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?

  • What type of values you want to validate, if the values are countable, try to use drop down and restrict user. – amit_game Dec 17 '15 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) – Omry Zobel Dec 17 '15 at 10:50

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>
   <comment>You will have to sign in after you save your custom admin path.</comment>

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



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>
    <comment>Specify URL or {{base_url}} placeholder.</comment>

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.


try this

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

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

enter preformatted text here

  • how to add asterisk mark (*) after label? – Rajeev Singh Apr 23 '19 at 12:23

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

    <label>Text Value</label>

for custom validation go through the below URL -


  • your answer is for magento 1. The op asked about magento 2 – Marius Dec 17 '15 at 12:13
  • oh ! i will update it soon, thanks @Marius – amit_game Dec 17 '15 at 15:53

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.