Skip to content

Commit 014a30d

Browse files
committed
MAGETWO-71669: Incorrect date format with Arabic language locale
- Update logic of beforeSave method;
1 parent b2907f5 commit 014a30d

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

app/code/Magento/Customer/Block/Widget/Dob.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ public function getHtmlId()
208208
*/
209209
public function getDateFormat()
210210
{
211-
return $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
211+
/** Escape invisible characters which are present in some locales and may corrupt formatting */
212+
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
213+
$escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);
214+
215+
return $escapedDateFormat;
212216
}
213217

214218
/**

app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,45 @@ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface
3737
public function beforeSave($object)
3838
{
3939
$attributeName = $this->getAttribute()->getName();
40-
$pattern = '/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/';
41-
// format only date that was not formatted yet
42-
if ($object->hasData($attributeName) && !preg_match($pattern, $object->getData($attributeName))) {
43-
try {
44-
$value = $this->formatDate($object->getData($attributeName));
45-
} catch (\Exception $e) {
46-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
47-
}
40+
$attributeValue = $object->getData($attributeName);
41+
if ($object->hasData($attributeName)) {
42+
// format only date that is not formatted yet
43+
$dateFormatted = $this->dateIsFormatted($attributeValue);
44+
if (!$dateFormatted) {
45+
try {
46+
$value = $this->formatDate($attributeValue);
47+
} catch (\Exception $e) {
48+
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
49+
}
4850

49-
if (is_null($value)) {
50-
$value = $object->getData($attributeName);
51-
}
51+
if (is_null($value)) {
52+
$value = $attributeValue;
53+
}
5254

53-
$object->setData($attributeName, $value);
55+
$object->setData($attributeName, $value);
56+
}
5457
}
5558

5659
return $this;
5760
}
5861

62+
/**
63+
* Check if date is formatted
64+
*
65+
* @param string|\DateTime $attributeValue
66+
* @return bool
67+
*/
68+
private function dateIsFormatted($attributeValue)
69+
{
70+
$pattern = '/(\d{4})-(\d{2})-(\d{2})(\s(\d{2}):(\d{2}):(\d{2}))?/';
71+
if ($attributeValue instanceof \DateTime) {
72+
return false;
73+
} elseif (preg_match($pattern, $attributeValue)) {
74+
return true;
75+
}
76+
return false;
77+
}
78+
5979
/**
6080
* Prepare date for save in DB
6181
*

0 commit comments

Comments
 (0)