0

I have Magento CE 1.9.2.4 installed in my system.

Now I want to validate my module's system configuration when "Save Config" button is clicked.

Below is my Observer code:

<?php
class Mycompany_StoreLocator_Model_Observer extends Varien_Event_Observer
{
    public function validateDefaultCountry($observer) {
        echo "<pre/>";print_r($observer->getEvent());die;
    }
}

Below is my config.xml:

<?xml version="1.0"?>
<!--
/**
 * Mycompany_Mymodule extension
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category   Mycompany
 * @package    Mycompany_Mymodule
 * @copyright  Copyright (c) 2008 Mycompany LLC
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * @category   Mycompany
 * @package    Mycompany_Mymodule
 * @author     Boris (Moshe) Gurevich <moshe@Mycompany.com>
 */
-->
<config>
    <modules>
        <Mycompany_Mymodule>
            <version>0.2.6</version>
        </Mycompany_Mymodule>
    </modules>
    <global>
        <models>
            <mymodule>
                <class>Mycompany_Mymodule_Model</class>
                <resourceModel>mymodule_mysql4</resourceModel>
            </mymodule>
            <mymodule_mysql4>
                <class>Mycompany_Mymodule_Model_Mysql4</class>
                <entities>
                    <location>
                        <table>mymodule_location</table>
                    </location>
                </entities>
            </mymodule_mysql4>
        </models>
        <events>
            <admin_system_config_changed_section_mymodule>
                <observers>
                    <mymodule>
                        <type>singleton</type>
                        <class>mymodule/observer</class>
                        <method>validateDefaultCountry</method>
                    </mymodule>
                </observers>
            </admin_system_config_changed_section_mymodule>
        </events>
        <resources>
            <mymodule_setup>
                <setup>
                    <module>Mycompany_Mymodule</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mymodule_setup>
            <mymodule_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mymodule_write>
            <mymodule_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mymodule_read>
        </resources>
        <helpers>
            <mymodule><class>Mycompany_Mymodule_Helper</class></mymodule>
        </helpers>
        <blocks>
            <mymodule><class>Mycompany_Mymodule_Block</class></mymodule>
        </blocks>
        <mymodule>
            <private_fields></private_fields>
        </mymodule>
    </global>
    <frontend>
        <routers>
            <mymodule>
                <use>standard</use>
                <args>
                    <module>Mycompany_Mymodule</module>
                    <frontName>mymodule</frontName>
                </args>
            </mymodule>
        </routers>
        <translate>
            <modules>
                <Mycompany_Mymodule>
                    <files>
                        <default>Mycompany_Mymodule.csv</default>
                    </files>
                </Mycompany_Mymodule>
            </modules>
        </translate>
          <layout>
              <updates>
                  <mymodule module="Mycompany_Mymodule">
                      <file>mymodule.xml</file>
                  </mymodule>
              </updates>
          </layout>
    </frontend>
    <admin>
         <routers>
            <mymoduleadmin>
                <use>admin</use>
                <args>
                    <module>Mycompany_Mymodule</module>
                    <frontName>mymoduleadmin</frontName>
                </args>
            </mymoduleadmin>
        </routers>
    </admin>
    <adminhtml>
        <menu>
          <mycompany>
             <title>Mycompany</title>
                <sort_order>71</sort_order>
                <children>
                    <mymodule translate="title" module="mymodule">
                        <title>Advance Store Locator</title>
                        <sort_order>3</sort_order> 
                        <action>mymoduleadmin/adminhtml_location</action>
                    </mymodule>
                 </children>
           </mycompany>
    </menu>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <mymodule translate="title" module="mymodule"> 
                                            <title>Advance Store Locator</title>
                                            <sort_order>50</sort_order>
                                        </mymodule>
                                    </children>                         
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
        <translate>
            <modules>
                <Mycompany_Mymodule>
                    <files>
                        <default>Mycompany_Mymodule.csv</default>
                    </files>
                </Mycompany_Mymodule>
            </modules>
        </translate>
    </adminhtml>
    <default>
        <mymodule>
            <general>
                <google_geo_url><![CDATA[https://maps.google.com/maps/geo]]></google_geo_url>
                <show_search>1</show_search>
                <show_map>0</show_map>
            </general>
        </mymodule>
    </default>
</config>

How can I get the system config field which is being saved when is Observer is triggered ?

Just like getting quote from checkout event $observer->getEvent()->getQuote()

1
2

you don't need to add an observer for that.
For system->config values you can use a backend model.
In system.xml where you declare your field do this:

<field_name_here translate="label">
    <label>Label here</label>
    <frontend_type>text</frontend_type>
    <backend_model>[module]/something_here</backend_model><!-- add this one -->
    <sort_order>10</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
</field_name_here>

Then you create the model [Namespace]_[Module]_Model_Something_Here

<?php 
class [Namespace]_[Module]_Model_Something_Here extends Mage_Core_Model_Config_Data
{
    protected function _beforeSave()
    {   
        //get the value being saved
        $value = $this->getValue();
        $isValid = your custom validation here
        if (!$isValid) {
            Mage::throwException(
                Mage::helper('[module]')->__('your error message here')
            );
        }

    }
}

This will prevent the value from saving in case it is wrong and you will see an error message at the top of the page.
You can see a working example for the field base_url from app/code/core/Mage/Core/etc/system.xml that uses the backend model adminhtml/system_config_backend_baseurl which translates to the class Mage_Adminhtml_Model_System_Config_Backend_Baseurl

0

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.