4

I want to add minimum value attribute for a admin interface field in my custom module. Can anyone suggest me how I can add min attribute in field? Here is my system.xml code:

<field id="uspsintexpress" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
<label>USPS Int'l Express Days</label>
</field>
2

Option 1

You can add minimum value validation by backend_model, for example.

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

Then you can find the beforeSave method at \m9agento210\vendor\magento\module-config\Model\Config\Backend\Baseurl.php

public function beforeSave()
{
    $value = $this->getValue();
    try {

       // You can write some validation here.

    } catch (\Magento\Framework\Exception\LocalizedException $e) {

        throw $error;
    }
}

This is an example, you can implement this in your custom module.

Option 2

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

Some Important validation classes are

'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 johndoe@domain.com.'
'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.'

Hope it helps.

  • Thanks Krishna, ie. Okay. is there not any way to validate field directly in system.xml. I just need to add min='1' in my admin XML file ? – Pankaj Sharma Jan 27 '17 at 9:20
  • @ Pankaj Sharma, As of my knowledge, there is no direct config validation like min='1' . You can follow the custom validation options added in the answer. – Krishna ijjada Jan 27 '17 at 9:23
  • Okay fine, if I proceed with option 2. Do you have any idea, how I can create a validation method for my requirement? value should be grater then 0. Remember I am creating a custom model. – Pankaj Sharma Jan 27 '17 at 10:01
  • You can simple go with option one by creating custom validation and return response. Please check the core validation once I added in the answer. This may be the quick approach. – Krishna ijjada Jan 27 '17 at 10:04
1

Finally, I got Solution for my Question. Here is solution system.xml code:

<field id="uspsintexpress" translate="label" type="text" sortOrder="70" showInDefault="1" showInWebsite="1" showInStore="1">
<label>USPS Int'l Express Days</label>
<validate>validate-greater-than-zero</validate>
</field>

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.