3

I want to prevent customers entering PO Boxes into the shipping address for selected Shipping Methods (UPS specifically in this case). I could override js/prototype/validation.js to insert a new validation pattern, but I don't want to fork such a key file.

Is there a mechanism to unobtrusively validate the customer's shipping address AFTER they select a shipping method via Javascript without overriding core files?

I see that Validation.add is used inside the validation.js, so it may be possible to add a new validation method outside of the core file?

The regex that I want to apply is:

\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)

If the validation cannot be performed elegantly in the JS, I would be interested in an Observer on the controller_action_predispatch_onepage_saveShippingMethod that inspects the data and performs an Ajax redirect back to the shipping address form if necessary.

starball
  • 20,030
  • 7
  • 43
  • 238
Jonathan Day
  • 18,519
  • 10
  • 84
  • 137

2 Answers2

7

The library used is Really Easy Field Validation and that page does explain how to extend it. I guess you will need something like this:

Validation.add('address', 'Error message text', {
    pattern : /\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)/
});
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • brilliant answer. With this method I was hoping to add a regex for UK postcodes but it does not seem you can have more than one validate- class on a field, so I have an 'alert' box instead, only triggering if the country is UK. The regex I used was from the wikipedia postcode page and I splice a space in if there is not one 3 chrs from the end as well as regexing it. – ʍǝɥʇɐɯ Dec 06 '11 at 14:14
  • Re: "only one validate- class on a field". I was unaware of that and will have to investigate closer. – clockworkgeek Dec 06 '11 at 15:57
  • @wehtam the documentation for the JS Validation that clockworkgeek linked to includes the ability to "include" other validations, so you could define your validate-uk-postcode to include validate-other-condition as well. – Jonathan Day Dec 07 '11 at 00:42
3

As a brief look into it without debugging the checkout

# Unfortunately Magento 1.3.2.3 - Find real postcode from debugging checkout
    public function saveShippingAction()

        {
            $this->_expireAjax();
            if ($this->getRequest()->isPost()) {
                $data = $this->getRequest()->getPost('shipping', array());
                $customerAddressId = $this->getRequest()->getPost('shipping_address_id', false);
                $result = $this->getOnepage()->saveShipping($data, $customerAddressId);

                $preg_search = '\b([P|p](OST|ost)?\.?\s?[O|o|0](ffice|FFICE)?\.?\s)?([B|b][O|o|0][X|x])\s(\d+)';
                $address = $this->getQuote()->getShippingAddress(); #find real postcode

                if(preg_match($preg_search, $address['postcode']){
                    $result = array(
                            'error' => 1,
                            'message' => Mage::helper('checkout')->__('Invalid PO Box postcode');
                        );
                }
                else{
                        if (!isset($result['error'])) {
                            $result['goto_section'] = 'shipping_method';
                            $result['update_section'] = array(
                                'name' => 'shipping-method',
                                'html' => $this->_getShippingMethodsHtml()
                            );
                        }
                }

                $this->getResponse()->setBody(Zend_Json::encode($result));
            }
        }