问题
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');
in addition to:
public function hookActionValidateCustomerAddressForm($data) { /* Here is the triggered hook */ }
But the triggered hook hookActionValidateCustomerAddressForm
is executed even when the user submits bad data (e.g. postcode with at least one letter).
It means that my program doesn't wait for Prestashop's data verification.
What is the way to execute this hook after Prestashop decides if the data is correct?
回答1:
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();
}
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.
来源:https://stackoverflow.com/questions/60429732/actionvalidatecustomeraddressformafter-triggers-the-hook-before-prestashops-for