简体   繁体   中英

Want to do server side validation in system.xml in Magento system configuration

I am new to Magento..I have created a custom configuration page in admin panel under system configuration. I have given some fields using system.xml file. Now I want to do server side validation for phone number column, but i am struggling to do..

I have given the following code in the following path: app/code/local/Envato/CustomConfig/etc/config.xml

   <?xml version="1.0"?>
  <config>
<modules>
    <Envato_CustomConfig>
        <version>0.0.1</version>
    </Envato_CustomConfig>
</modules>
<global>
    <helpers>
        <customconfig>
            <class>Envato_CustomConfig_Helper</class>
        </customconfig>
    </helpers>
    <models>
         <customconfig>
            <class>Envato_CustomConfig_Model</class>
         </customconfig>
    </models>
</global>
<adminhtml>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <customconfig_options>
                                        <title>Custom Configuration Section</title>
                                    </customconfig_options>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</adminhtml>
 </config>

My system.xml file: code/local/Envato/CustomConfig/etc/system.xml:

      <?xml version="1.0"?>
   <config>
  <tabs>
    <customconfig translate="label" module="customconfig">
        <label>Custom Configuration Tab</label>
        <sort_order>1</sort_order>
      </customconfig>
  </tabs>

  <sections>
    <customconfig_options translate="label" module="customconfig">
        <label>Custom Configuration Settings</label>
        <tab>customconfig</tab>
        <frontend_type>text</frontend_type>
        <sort_order>1</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
        <groups>
            <section_one translate="label">
                <label>Section One</label>
                <frontend_type>text</frontend_type>
                <sort_order>1</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>               
                <fields>
                    <custom_field_one>
                        <label>Custom Text Field</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of text field.      </comment>                  
                    </custom_field_one>
                    <custom_field_two>
                        <label>Image</label>
                        <frontend_type>image</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_two>
                    <custom_field_three>
                        <label>Email Id</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_three>
                    <custom_field_four>
                        <label>Mobile Number</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <validate>validate-number</validate>
           <backend_model>customconfig/number</backend_model> 
                        <sort_order>4</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_four>
                    <custom_field_five>
                        <label>Password</label>
                        <frontend_type>password</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>
                    </custom_field_five>
                    <custom_field_six>
                        <label>Booking Date</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>6</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_six>
                </fields>
            </section_one>
            <section_two translate="label">
                <label>Section Two</label>
                <frontend_type>text</frontend_type>
                <sort_order>2</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>               
                <fields>
                    <custom_field_two>
                        <label>Custom Select Field</label>
                        <frontend_type>select</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of select field.</comment>
                    </custom_field_two>
                    <custom_field_three>
                        <label>Custom Radio Field</label>
                        <frontend_type>radios</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of radios field.</comment>
                    </custom_field_three>
                    <custom_field_four>
                        <label>Custom Multiselect Field</label>
                        <frontend_type>multiselect</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of multiselect field.</comment>
                    </custom_field_four>
                </fields>
            </section_two>
        </groups>               
    </customconfig_options>
</sections>
 </config>

My Helper file : /app/code/local/Envato/CustomConfig/Helper/Data.php

  <?php

  class Envato_CustomConfig_Helper_Data extends Mage_Core_Helper_Abstract
   {
    }

My Model file: app/code/local/Envato/CustomConfig/Model/Options.php

      <?php
        class Envato_CustomConfig_Model_Options
       {
      public function toOptionArray()
         {
 return array(
  array('value'=>1, 'label'=>'One'),
  array('value'=>2, 'label'=>'Two'),
  array('value'=>3, 'label'=>'Three'),           
  array('value'=>4, 'label'=>'Four')                    
 );
   }
   }

My Model file: app/code/local/Envato/CustomConfig/Model/Options.php

      <?php
     class Envato_CustomConfig_Model_Number extends Mage_Core_Model_Abstract
       {
public function save()
{   
    $number = $this->getValue(); //get the value from our config
    $number = preg_replace('#[^0-9]#','',$number); //strip non numeric
    if(strlen($number) < 10)   //exit if we're less than 10 digits long
    {
         Mage::getSingleton('core/session')->addNotice('Phone Numbers need 10 digits.');
    }

    return parent::save();  

    }
     }

My tag is working in system.xml file. But I get the error message with data store.. I want to do server side validation before data gets stored into db... Can Anyone help to do server side validation for those fields..

Thanks in Advance......

I think there is issue in your model class. Please check this detailed tutorial:

http://alanstorm.com/magento_system_config_validation

Updated:

class Envato_CustomConfig_Model_Number extends Mage_Core_Model_Config_Data{


public function save() {   
        $number = $this->getValue(); //get the value from our config
        $number = preg_replace('#[^0-9]#','',$number); //strip non numeric
        if(strlen($number) < 10)   //exit if we're less than 10 digits long
        {
             Mage::getSingleton('core/session')->addError('Phone Numbers need 10 digits.');
             return true;
        }

        return parent::save();  
    }
}

Use this model .. It will not save data until it is valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM