http://www.webbitten.com
Adding validation to registration form in Magento
Written by Mohit D   

As the name suggests ,in this article we are going to see how we can add custom validation to any of the fields in the registration form in magento .I have come across a lot of requests for adding a post-code based validation for a specific country.

So through a post-code validation i am going to show you how you can add "server side validation" at the checkout step in Magento.

The base class used for validation is Mage_Customer_Model_Address_Abstract and this is extended by a class Mage_Sales_Model_Quote_Address at the checkout page during address' validation.The function that performs the validation is validate() . By default ,It is not defined in Mage_Sales_Model_Quote_Address and thus the validate() method of class Mage_Customer_Model_Address_Abstract is used.

Now we have spotted the Model (Mage_Sales_Model_Quote_Address)  that can override the validate() function of it's base class ( Mage_Customer_Model_Address_Abstract  ) . But as you must have already learned in many other magento tutorials , it's not a good practice to change the code of the "/core/" files.So we will override the Model class (Mage_Sales_Model_Quote_Address)  and define our own validate() function  which will override the validate() function in the class  Mage_Customer_Model_Address_Abstract. I hope that's clear,if not ,then jump right into the code,who cares for the lousy explanation anyway!

 

For overriding the model we will create a module,let's have "Webbitten" as  namespace  and "Sales" as module name under the "local" code pool.

The directory structure would be

/ local

/ Webbitten

/ Sales

/ Model

/ Quote / Address.php

/ etc / config.xml

And for validation  :

/ lib

/ Zend

/ Validate /PostCode.php

 

Here is the code for Address.php file :

<pre class=”brush: php; ruler: true; first-line: 10;”>

<?php

class Webbitten_Sales_Model_Quote_Address extends Mage_Sales_Model_Quote_Address
{
public function validate()
{

$errors = array();


$helper = Mage::helper('customer');

//    if($this->getData('address_type')=='shipping')  // if you want to check only shipping postcode
//{

if (!Zend_Validate::is($this->getPostcode(), 'PostCode'))  // The second parameter (i.e 'PostCode')  refers to the php file at " / lib / Zend / Validate /PostCode.php".

{
$errors[] = $helper->__('Please enter a valid post code.');
}
//}

if (empty($errors)){
return true;
}


return $errors;    

}

}

</pre>

Here is the file PostCode.php which has the core logic for postcode validation :

<pre class=”brush: php; ruler: true; first-line: 10;”>

<?php

class Zend_Validate_PostCode extends Zend_Validate_Abstract
{
const IS_CORRECT = 'isCorrect';

/**
* @var array
*/
protected $_messageTemplates = array(
self::IS_CORRECT => "The post code is not correct.Please check again..."
);

/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if $value is a correct post code.
*
* @param  string $value
* @return boolean
*/
public function isValid($value)          //  The returned value i.e  true or false(boolean)  by this function decides if the postcode is valid(true) or not (false).
{
$this->_setValue((string) $value);

if (!preg_match('/^[0-9]{6}$/', $value)) // This(regular expression) is just for this example,different countries have different rules for post codes.Change it with yours.
{

// the case when the condition is violated
$this->_error();
return false;
}

// return true is the post code is valid
return true;
}

}

 

</pre>

 

Here is the config.xml which tells magento which class in local code pool extends it's core class :

<pre class=”brush: php; ruler: true; first-line: 10;”>

<?xml version="1.0"?>

<config>
<modules>
<Webbitten_Sales>
<version>0.0.1</version>
</Webbitten_Sales>
</modules>
<global>
<models>
<sales>
<rewrite>
<quote_address>Webbitten_Sales_Model_Quote_Address</quote_address>
</rewrite>
</sales>
</models>
</global>
</config>

</pre>

Now , to register this module with magento we will have to place a "Webbitten_Sales.xml"(i.e namespace_modulename.xml)  under the directory "/app/etc/modules/".

Here's what "Webbitten_Sales.xml"  looks like :

<pre class=”brush: php; ruler: true; first-line: 10;”>

<?xml version="1.0"?>

<config>
<modules>
<Webbitten_Sales>
<active>true</active>
<codePool>local</codePool>
</Webbitten_Sales>
</modules>
</config>

</pre>

 

I hope that helps someone out there : ) .Any questions , comments ,suggestions or corrections are welcomed.

 
http://www.webbitten.com, Powered by Joomla!; Joomla templates by SG web hosting