prestashop - actionValidateCustomerAddressFormAfter 在 Prestashop 的表单验证之前触发钩子
问题描述
在我的 Prestashop 1.7 网站中,我有一个前端地址编辑表单(允许我的客户编辑其邮政地址)。在 Prestashop 确定用户输入的数据是否正确(例如,邮政编码仅包含数字)之后,我想执行一个钩子。我认为可以通过使用来完成:
$this->registerHook('actionValidateCustomerAddressFormAfter');
此外:
public function hookActionValidateCustomerAddressForm($data) { /* Here is the triggered hook */ }
但是hookActionValidateCustomerAddressForm
即使用户提交了错误的数据(例如至少有一个字母的邮政编码),触发的钩子也会被执行。
这意味着我的程序不等待 Prestashop 的数据验证。
Prestashop 判断数据是否正确后执行这个钩子的方法是什么?
解决方案
这个钩子在类的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 相同的验证。
推荐阅读
- vue.js - 如何在 vuejs 中为 doc 文件制作下载链接按钮
- odoo - 如何在库存报告odoo13中显示当前库存和库存的相同视图
- flutter - Flutter - 如何在所有边缘上获得线性渐变?
- reactjs - 在reactjs中使用来自前端的预签名URL上传文件
- rabbitmq - MassTransit 交换以交换绑定优势
- javascript - 无法设置未定义角度表的属性
- winui - WinUi 3 中的渲染模型是什么?
- django - 在 django 中显示多维数据
- javascript - reactjs(函数组件)中的 setInterval 是如何工作的?
- android - 使用脚本定期从 google-play 导出数据