Adminhtml Form Field custom validation function via Prototype Js

Step 1. In your Form element add custom validate class

 
<?php
 
class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
 
    protected function _prepareForm()
    {
	...
        $fieldset->addField('custom_id', 'text', array(
            'label'     => Mage::helper('modulename')->__('Custom ID'),
            'class'     => 'validate-custom-id',
            'required'  => true,
            'name'      => 'custom_id'
        ));
	...

Step 2: Add the following to your extension’s app/code/local/Vendorname/Modulename/Block/Adminhtml/Modulename/Edit.php form block Edit file.
Magento uses Prototype library for validation.

 
<?php
 
class Vendorname_Modulename_Block_Adminhtml_Modulename_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
    public function __construct()
    {
        $this->_objectId = 'id';
        $this->_blockGroup = 'modulename';
        $this->_controller = 'adminhtml_modulename';
        $this->_mode = 'edit';
 
        parent::__construct();
 
        $this->_formInitScripts[] = '
            //<![CDATA[
            Validation.add(\'validate-custom-id\', \'Custom ID not selected.\', function(v) {
		return Validation.get('IsEmpty').test(v)
            })
           //]]>
           ';
    }

Step 3. In your extension, Adminhtml Form, when you click Save the field should throw a validation error if it’s empty

What is reference for in layout file?

Reference means you are going to using the already defined block.

By default all pages has following blocks:
Header, Left, Content, Right, Footer

Pass information from Observer via session

Step 1:

            Mage::dispatchEvent('my_event');

Step 2:
link method MyEvent to event “my_event” in your module’s config.xml file
Observer.php

    public function MyEvent(Varien_Event_Observer $observer) {
            Mage::getSingleton('customer/session')->setData('my_event', 'results');
    }
}

Step 3:

$result = Mage::getSingleton('customer/session')->getData('my_event'));

Load Sales/quote by id returns empty data

$quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);

instead of

$quote = Mage::getModel('sales/quote')->load($quoteId);

because the regular Mage_Sales_Model_Quote::load() function automatically attaches the current storeview to load the quote object. In this case the storeview is admin, which means no results will be found for the quote id and the returned object is empty.

Using date and time

echo Mage::getModel('core/date')->date('d/m/Y');
echo Mage::getModel('core/date')->timestamp(time());
Mage::getSingleton('core/date')->date(Varien_Date::DATETIME_PHP_FORMAT));
Mage::getModel('core/date')->gmtDate();

Country list

        $countryList = Mage::getModel('directory/country')
            ->getCollection()
            ->loadData()
            ->toOptionArray(false);
 
        echo '<pre>';
        print_r($countryList);

Magento system configuration hierarchy

Default->Website->Store->Store View
Store View not used in Magento System Configuration

system.xml

...
	<show_in_default>0</show_in_default>
	<show_in_website>1</show_in_website>
	<show_in_store>0</show_in_store> 
...

Change layout

yourlayout.xml

	<my_template_page>
		<reference name="root">
			<action method="setTemplate"><template>page/my-layout-type-2.phtml</template></action>
		</reference>
		<reference name="content">
				<block type="my/template" name="text_message" template="my/template.phtml"/>
		</reference>
	</mutual_template_page>

Get Customer Data

<?php
$customer = Mage::getModel('customer/customer')->load($customerId);
if ($customer->getId()) {
   var_dump($customer->getFirstname()); 
   var_dump($customer->getEmail());
   var_dump($customer->getData()); 
}

Send Transactional Email

 
// SELECT template_id FROM core_email_template WHERE template_code='Your template code';
$templateId = 495;
 
$sender = array('name' => 'Imran Aghan',
    'email' => 'email@address.co.uk');
 
 
// Set recepient information
$recepientEmail = 'someone@someaddress.co.uk';
$recepientName = 'Name Surname';
 
// Get Store ID
$storeId = Mage::app()->getStore()->getId();
 
// Send Transactional Email
Mage::getModel('core/email_template')
    ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName,
        array(
            'variable1' => 'Customer Name',
            'variable2' => 'Customer Surname'
        ),
        $storeId);