如何不引人注目地向Magento checkout添加新的验证方法

时间:2022-05-06 16:17:40

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.

我想阻止客户将邮政信箱输入所选运输方式的运送地址(特别是在这种情况下为UPS)。我可以覆盖js / prototype / validation.js以插入新的验证模式,但我不想分叉这样的密钥文件。

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

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

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?

我看到在validation.js中使用了Validation.add,因此可以在核心文件之外添加新的验证方法?

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.

如果无法在JS中优雅地执行验证,我会对controller_action_predispatch_onepage_saveShippingMethod上的Observer感兴趣,它会检查数据并在必要时执行Ajax重定向回送货地址表单。

2 个解决方案

#1


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:

使用的库是Really Easy Field Validation,该页面确实解释了如何扩展它。我想你需要这样的东西:

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+)/
});

#2


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));
            }
        }

#1


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:

使用的库是Really Easy Field Validation,该页面确实解释了如何扩展它。我想你需要这样的东西:

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+)/
});

#2


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));
            }
        }