actionValidateCustomerAddressFormAfter triggers the hook before Prestashop's form validation

旧街凉风 提交于 2020-05-17 05:46:52


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?


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())) {
                    'Invalid postcode - should look like "%zipcode%"',
                    array('%zipcode%' => $country->zip_code_format),
                $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.