Unverified Commit a42f25be authored by Krystian Podemski's avatar Krystian Podemski Committed by GitHub
Browse files

Merge pull request #28028 from atomiix/fix/27876

Remove duplicate error messages in account and address creation
parents fc2b3d5d 0c83e5c0
......@@ -104,7 +104,20 @@ class CustomerAddressFormCore extends AbstractForm
public function validate()
{
$is_valid = $this->validateFieldsValues();
$is_valid = true;
$postcode = $this->getField('postcode');
if ($postcode && $postcode->isRequired()) {
$country = $this->formatter->getCountry();
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 (($hookReturn = Hook::exec('actionValidateCustomerAddressForm', ['form' => $this])) !== '') {
$is_valid &= (bool) $hookReturn;
......@@ -140,16 +153,10 @@ class CustomerAddressFormCore extends AbstractForm
$this->setAddress($address);
try {
return $this->getPersister()->save(
$address,
$this->getValue('token')
);
} catch (PrestaShopException $e) {
$this->errors[''][] = $this->translator->trans('Could not update your information, please check your data.', [], 'Shop.Notifications.Error');
}
return false;
return $this->getPersister()->save(
$address,
$this->getValue('token')
);
}
/**
......@@ -209,82 +216,4 @@ class CustomerAddressFormCore extends AbstractForm
'formFields' => $formFields,
];
}
/**
* Performs validation on field values.
* Returns true if all field values are correct, false otherwise.
*
* @return bool
*/
private function validateFieldsValues(): bool
{
$isValid = true;
$isValid &= $this->validatePostcode();
$isValid &= $this->validateField('firstname', 'isName', $this->translator->trans(
'Invalid name',
[],
'Shop.Forms.Errors'
));
$isValid &= $this->validateField('lastname', 'isName', $this->translator->trans(
'Invalid name',
[],
'Shop.Forms.Errors'
));
$isValid &= $this->validateField('city', 'isCityName', $this->translator->trans(
'Invalid format.',
[],
'Shop.Forms.Errors'
));
return (bool) $isValid;
}
/**
* @return bool
*/
private function validatePostcode(): bool
{
$postcode = $this->getField('postcode');
if ($postcode && $postcode->isRequired()) {
$country = $this->formatter->getCountry();
if (!$country->checkZipCode($postcode->getValue())) {
$postcode->addError($this->translator->trans(
'Invalid postcode - should look like "%zipcode%"',
['%zipcode%' => $country->zip_code_format],
'Shop.Forms.Errors'
));
return false;
}
}
return true;
}
/**
* @param string $fieldName
* @param string $validationFunction
* @param string $validationFailMessage
*
* @return bool
*/
private function validateField(string $fieldName, string $validationFunction, string $validationFailMessage): bool
{
$field = $this->getField($fieldName);
if (null === $field) {
return true;
}
$value = $field->getValue();
if ($field->isRequired() && empty($value)) {
return false;
}
if (!empty($value) && false === (bool) Validate::$validationFunction($value)) {
$field->addError($validationFailMessage);
return false;
}
return true;
}
}
......@@ -149,9 +149,7 @@ class CustomerFormCore extends AbstractForm
'Shop.Notifications.Error'
));
}
$this->validateFieldsLengths();
$this->validateFieldsValues();
$this->validateByModules();
return parent::validate();
......@@ -213,17 +211,12 @@ class CustomerFormCore extends AbstractForm
$clearTextPassword = $this->getValue('password');
$newPassword = $this->getValue('new_password');
try {
$ok = $this->customerPersister->save(
$this->getCustomer(),
$clearTextPassword,
$newPassword,
$this->passwordRequired
);
} catch (PrestaShopException $e) {
$this->errors[''][] = $this->translator->trans('Could not update your information, please check your data.', [], 'Shop.Notifications.Error');
$ok = false;
}
$ok = $this->customerPersister->save(
$this->getCustomer(),
$clearTextPassword,
$newPassword,
$this->passwordRequired
);
if (!$ok) {
foreach ($this->customerPersister->getErrors() as $field => $errors) {
......@@ -282,35 +275,4 @@ class CustomerFormCore extends AbstractForm
}
}
}
/**
* Performs validation on field values.
* Adds error to the field object if value is not as expected.
*/
private function validateFieldsValues(): void
{
$this->validateFieldIsCustomerName('firstname');
$this->validateFieldIsCustomerName('lastname');
}
/**
* Checks whether a field's value is a valid customer(person) name.
*
* @param string $fieldName
*/
private function validateFieldIsCustomerName(string $fieldName): void
{
$field = $this->getField($fieldName);
if (null === $field) {
return;
}
$value = $field->getValue();
if (!empty($value) && false === (bool) Validate::isCustomerName($value)) {
$field->addError($this->translator->trans(
'Invalid format.',
[],
'Shop.Forms.Errors'
));
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment