actionValidateCustomerAddressFormAfter在Prestashop的表单验证之前触发钩子 [英] actionValidateCustomerAddressFormAfter triggers the hook before Prestashop's form validation

查看:88
本文介绍了actionValidateCustomerAddressFormAfter在Prestashop的表单验证之前触发钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Prestashop 1.7 Website, I have a front-end address edition form (allowing my customer to edit its postal addresses). I want to execute a hook after Prestashop has decided weither the data the user typed are correct or not (e.g. postcode contains digits only). I thought it could be done by using:

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

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

But the triggered hook hookActionValidateCustomerAddressForm is executed even when the user submits bad data (e.g. postcode with at least one letter).

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

It means that my program doesn't wait for Prestashop's data verification.

在Prestashop决定数据是否正确之后,执行此挂接的方法是什么?

What is the way to execute this hook after Prestashop decides if the data is correct?

推荐答案

此钩子是在CustomerAddessForm类的validate()函数中执行的:

This hook is executed in validate() function from CustomerAddessForm class:

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进行相同的验证.

This hook is executed always (no matter if form is valid or not) and PrestaShop doesn't send you $is_valid as a parameter. So the only thing that you can do is to do the same validation than PrestaShop if you want to know if form is valid.

这篇关于actionValidateCustomerAddressFormAfter在Prestashop的表单验证之前触发钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆