首页 > 解决方案 > actionValidateCustomerAddressFormAfter 在 Prestashop 的表单验证之前触发钩子

问题描述

在我的 Prestashop 1.7 网站中,我有一个前端地址编辑表单(允许我的客户编辑其邮政地址)。在 Prestashop 确定用户输入的数据是否正确(例如,邮政编码仅包含数字)之后,我想执行一个钩子。我认为可以通过使用来完成:

$this->registerHook('actionValidateCustomerAddressFormAfter');此外: public function hookActionValidateCustomerAddressForm($data) { /* Here is the triggered hook */ }

但是hookActionValidateCustomerAddressForm即使用户提交了错误的数据(例如至少有一个字母的邮政编码),触发的钩子也会被执行。

这意味着我的程序不等待 Prestashop 的数据验证。

Prestashop 判断数据是否正确后执行这个钩子的方法是什么?

标签: prestashophookprestashop-1.7

解决方案


这个钩子在类的validate()函数中执行CustomerAddessForm

public function validate()
{
    $is_valid = true;

    if (($postcode = $this->getField('postcode'))) {
        if ($postcode->isRequired()) {
            $country = $this->formatter->getCountry();
            if (!$country->checkZipCode($postcode->getValue())) {
                $postcode->addError($this->translator->trans(
                    'Invalid postcode - should look like "%zipcode%"',
                    array('%zipcode%' => $country->zip_code_format),
                    'Shop.Forms.Errors'
                ));
                $is_valid = false;
            }
        }
    }

    if (($hookReturn = Hook::exec('actionValidateCustomerAddressForm', array('form' => $this))) !== '') {
        $is_valid &= (bool) $hookReturn;
    }

    return $is_valid && parent::validate();
}

这个钩子总是执行(无论表单是否有效)并且 PrestaShop 不会将您$is_valid 作为参数发送。因此,如果您想知道表单是否有效,您唯一能做的就是进行与 PrestaShop 相同的验证。


推荐阅读