4

I have already followed this question for front-end validation of PostCode : Zip/Postal Code validation not working properly

How can i add a server-side validation? Thank you.

4

You can use the event customer_address_validation_after. This event is fired during the validate function on the address model and passed the address in question.

Mage::dispatchEvent('customer_address_validation_after', array('address' => $this));

What you could do here is check if the postcode attribute is numeric via what ever means you wish and then use the add error function available in the class Mage_Customer_Model_Address_Abstract.

Firstly you will need a module that has a models definition and an events definition in it's config.xml.

Then you will need to create your observer model. That currently will only have one function. The one defined to be used by the event in your config.xml.

Your code could look something like the following.

$address = $observer->getAddress();
if (postcode is not valid) {
    $address->addError(Mage::helper('customer')->__('Please enter a valid postcode'));
}

A few good links for understanding events:

  1. http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method
  2. http://inchoo.net/ecommerce/magento/magento-event-driven-programming-tips-tricks/
  3. http://www.pierrefay.com/event-observers-magento-tutorial-howto-105
  • Perfect! I'm new to Magento...code should be in app/code/core/Mage/Customer/controllers/AddressController.php. What's the best practice to override this script? Thank you. – Mario Villani Aug 26 '14 at 17:12
  • Do not override the controller. Use this event to add the validation. – David Manners Aug 26 '14 at 17:13
  • Where should the event be launched? – Mario Villani Aug 26 '14 at 17:18
  • I will write a link to events when I am off my phone. – David Manners Aug 26 '14 at 17:18
  • @Mario90 I have added some extra reading about events to the answer. – David Manners Aug 26 '14 at 17:52
1

I can think of at least one place to validate this on the server side. You can do this in the controller, so that you can redirect the customer back to the page with flashing a message, stating the a form field was incorrect.

In Mage_Customer_AddressController::formPostAction(), you can check for the post code post value and check if it's number. If not, redirect customer flash a message.

// Pseudocode

// After validating the form key...
$postcode = $this->getRequest()->getParam('code');
if (!is_numeric($postcode)) {
    // redirect to form/where user came from
}

// continue as usual

You can fill in the code. You'll also need to identify any other forms where post codes are entered. Since you haven't said specifically, this is one of them that you can change.

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.