如何不显眼地向 Magento 结帐添加新的验证方法?

发布于 2024-12-19 19:21:10 字数 532 浏览 1 评论 0原文

我想阻止客户在所选送货方式的送货地址中输入邮政信箱(在本例中尤其是 UPS)。我可以重写 js/prototype/validation.js 来插入新的验证模式,但我不想分叉这样的密钥文件。

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

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

我想要应用的正则表达式是:

\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 重定向回发货如有必要,填写地址表格。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2024-12-26 19:21:10

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

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

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+)/
});
2024-12-26 19:21:10

作为一个简短的研究,而不调试结账

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

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));
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文