Want to do server side validation in system.xml file in Magento config
I am new to Magento. I created a custom config page in the admin panel in the system config. I have given some fields using the system.xml file. Now I want to do server side validation for the phone number column, but I am struggling to do so.
I have provided the following code in the following path: Application / 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 works in the system.xml file. But I am getting an error with datastore. I want to do server side validation before the data is stored in the db ... Can anyone help me do server side validation for these fields.
Thanks in Advance ......
source to share
I think there is a problem in your model class. Check out 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.
source to share