9

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
13

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.

4

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

  • how to add asterisk mark (*) after label? – Rajeev Singh Apr 23 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

  • 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.