【问题标题】:How to unobtrusively add new Validation methods to Magento checkout如何在 Magento 结账时不显眼地添加新的验证方法
【发布时间】:2012-01-13 16:00:27
【问题描述】:

我想阻止客户将邮政信箱输入到所选运输方式的收货地址(本例中为 UPS)。我可以覆盖js/prototype/validation.js 以插入新的验证模式,但我不想分叉这样的密钥文件。

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

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

我要应用的正则表达式是

\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 重定向回送货地址表单。

【问题讨论】:

    标签: ajax validation magento shipping unobtrusive


    【解决方案1】:

    使用的库是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+)/
    });
    

    【讨论】:

    • 出色的答案。使用这种方法,我希望为英国邮政编码添加一个正则表达式,但似乎一个字段上不能有多个验证类,所以我有一个“警报”框,只有在国家是英国时才会触发。我使用的正则表达式来自维基百科邮政编码页面,如果最后没有一个 3 chrs 以及正则表达式,我会在其中拼接一个空格。
    • 回复:“一个字段上只有一个验证类”。我没有意识到这一点,将不得不进一步调查。
    • @wehtam clockworkgeek 链接到的 JS 验证文档包括“包含”其他验证的能力,因此您可以定义您的 validate-uk-postcode 以包含 validate-other-condition。
    【解决方案2】:

    在不调试结帐的情况下简要了解一下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多