New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check for required fields in BO > Country > Address format #15428
Changes from 4 commits
a7b7408
513df5c
f9632ac
c525225
449162e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,11 +236,55 @@ public function checkFormatFields() | |
} | ||
} | ||
} | ||
$this->checkRequiredFields($usedKeyList); | ||
} | ||
|
||
return (count($this->_errorFormatList)) ? false : true; | ||
} | ||
|
||
/** | ||
* Checks that all required fields exist in a given fields list. | ||
* Fills _errorFormatList array in case of absence of a required field. | ||
* | ||
* @param array $fieldList | ||
*/ | ||
protected function checkRequiredFields($fieldList) | ||
{ | ||
foreach (self::getFieldsRequired() as $requiredField) { | ||
if (!in_array($requiredField, $fieldList)) { | ||
$this->_errorFormatList[] = $this->trans( | ||
'The %s field (in tab %s) is required.', | ||
array($requiredField, $this->getFieldTabName($requiredField)), | ||
'Admin.Notifications.Error'); | ||
matthieu-rolland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
/** | ||
* Given a field name, get the name of the tab in which the field name can be found. | ||
* For ex: Country:name => the tab is 'Country'. | ||
* There should be only one separator in the string, otherwise return false. | ||
* | ||
* @param string $field | ||
* | ||
* @return bool|string | ||
*/ | ||
private function getFieldTabName($field) | ||
{ | ||
if (strpos($field, ':') === false) { | ||
// When there is no ':' separator, the field is in the Address tab | ||
return 'Address'; | ||
} | ||
|
||
$fieldTab = explode(':', $field); | ||
if (count($fieldTab) === 2) { | ||
// The part preceding the ':' separator is the name of the tab in which there is the required field | ||
return $fieldTab[0]; | ||
} | ||
|
||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have doubts on how to deal with this (when this function returns false). "Normally" it can never happen, but I wanted the method to be abnormal-proof. Do you think I should throw some kind of exception, in the legacy? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a general rule, methods should do what they are expected to do, or throw an exception otherwise. Return values should only reflect "correct" and "expected" states in normal execution, all other situations should be considered exceptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your input @eternoendless 👍 |
||
} | ||
|
||
/** | ||
* Returns the error list. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.