Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

magento-engcom/import-export-improvements#30: Validation should fail when a required field is supplied but is empty after trimming #113

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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