Skip to content

Commit

Permalink
ENGCOM-2237: magento-engcom/import-export-improvements#30: Validation…
Browse files Browse the repository at this point in the history
… should fail when a required field is supplied but is empty after trimming #113
  • Loading branch information
Stanislav Idolov authored Jul 18, 2018
2 parents ac0c755 + 795d6a4 commit c3ef7b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 12 additions & 2 deletions app/code/Magento/CustomerImportExport/Model/Import/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,18 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
if (in_array($attributeCode, $this->_ignoredAttributes)) {
continue;
}

$isFieldRequired = $attributeParams['is_required'];
$isFieldNotSetAndCustomerDoesNotExist =
!isset($rowData[$attributeCode]) && !$this->_getCustomerId($email, $website);
$isFieldSetAndTrimmedValueIsEmpty
= isset($rowData[$attributeCode]) && '' === trim($rowData[$attributeCode]);

if ($isFieldRequired && ($isFieldNotSetAndCustomerDoesNotExist || $isFieldSetAndTrimmedValueIsEmpty)) {
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
continue;
}

if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
$this->isAttributeValid(
$attributeCode,
Expand All @@ -603,8 +615,6 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
? $this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]
: Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
);
} elseif ($attributeParams['is_required'] && !$this->_getCustomerId($email, $website)) {
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function addError(
$errorMessage = null,
$errorDescription = null
) {
if ($this->isErrorAlreadyAdded($rowNumber, $errorCode)) {
if ($this->isErrorAlreadyAdded($rowNumber, $errorCode, $columnName)) {
return $this;
}
$this->processErrorStatistics($errorLevel);
Expand Down Expand Up @@ -333,13 +333,14 @@ public function clear()
/**
* @param int $rowNum
* @param string $errorCode
* @param string $columnName
* @return bool
*/
protected function isErrorAlreadyAdded($rowNum, $errorCode)
protected function isErrorAlreadyAdded($rowNum, $errorCode, $columnName = null)
{
$errors = $this->getErrorsByCode([$errorCode]);
foreach ($errors as $error) {
if ($rowNum == $error->getRowNumber()) {
if ($rowNum == $error->getRowNumber() && $columnName == $error->getColumnName()) {
return true;
}
}
Expand Down

0 comments on commit c3ef7b0

Please sign in to comment.