-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddressFormValidationTrait.php
More file actions
87 lines (79 loc) · 3.07 KB
/
AddressFormValidationTrait.php
File metadata and controls
87 lines (79 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* For the full copyright and license information, please view the
* docs/licenses/LICENSE.txt file that was distributed with this source code.
*/
namespace PrestaShop\Module\PsOnePageCheckout\Form;
use Country;
use Hook;
/**
* Trait for shared address form validation (postcode + hook).
* Used by CustomerAddressForm and OnePageCheckoutForm (legacy classes).
*/
trait AddressFormValidationTrait
{
/**
* Validate delivery and optionally invoice postcode.
*
* @param \FormField|null $postcode Delivery postcode field
* @param \Country|null $country Delivery country (used for delivery and as fallback for invoice)
* @param \FormField|null $invoicePostcode Invoice postcode field (null to skip)
* @param \FormField|null $invoiceCountryField Invoice country field (null to use $country for invoice)
*
* @return bool
*/
protected function validateAddressPostcode(
$postcode = null,
$country = null,
$invoicePostcode = null,
$invoiceCountryField = null,
) {
$is_valid = true;
if ($postcode && $country && $postcode->isRequired()) {
if (!$country->checkZipCode($postcode->getValue())) {
$postcode->addError($this->translator->trans(
'Invalid postcode - should look like "%zipcode%"',
['%zipcode%' => $country->zip_code_format],
'Shop.Forms.Errors'
));
$is_valid = false;
}
}
if ($invoicePostcode && $invoicePostcode->isRequired()) {
$invoiceCountry = $country;
if ($invoiceCountryField && $invoiceCountryField->getValue()) {
$invoiceCountry = new \Country(
(int) $invoiceCountryField->getValue(),
$this->language->id
);
}
if ($invoiceCountry) {
if ($invoiceCountry->need_zip_code && !$invoiceCountry->checkZipCode($invoicePostcode->getValue())) {
$invoicePostcode->addError($this->translator->trans(
'Invalid postcode - should look like "%zipcode%"',
['%zipcode%' => $invoiceCountry->zip_code_format],
'Shop.Forms.Errors'
));
$is_valid = false;
}
} elseif ($country && $country->checkZipCode($invoicePostcode->getValue()) === false) {
$invoicePostcode->addError($this->translator->trans(
'Invalid postcode - should look like "%zipcode%"',
['%zipcode%' => $country->zip_code_format],
'Shop.Forms.Errors'
));
$is_valid = false;
}
}
return $is_valid;
}
/**
* Run the address form validation hook.
*
* @return bool false if hook returned false, true otherwise
*/
protected function validateAddressFormHook()
{
return \Hook::exec('actionValidateCustomerAddressForm', ['form' => $this]) !== false;
}
}