3

我想阻止客户将 PO Boxes 输入到所选运输方式的收货地址中(在这种情况下特别是 UPS)。我可以重写js/prototype/validation.js以插入新的验证模式,但我不想分叉这样的密钥文件。

在客户通过 Javascript 选择送货方式而不覆盖核心文件后,是否有一种机制可以不显眼地验证客户的送货地址?

我看到它Validation.add在里面使用validation.js,所以可以在核心文件之外添加一个新的验证方法吗?

我要应用的正则表达式是

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

如果验证不能在 JS 中优雅地执行,我会对一个观察者感兴趣,controller_action_predispatch_onepage_saveShippingMethod它检查数据并在必要时执行 Ajax 重定向回送货地址表单。

4

2 回答 2

7

使用的库是真正容易的字段验证,该页面确实解释了如何扩展它。我想你会需要这样的东西:

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+)/
});
于 2011-12-06T00:59:34.703 回答
3

在不调试结帐的情况下简要介绍一下

# 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));
            }
        }
于 2011-12-06T01:36:07.183 回答