Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Date.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class Date extends AbstractData
13 {
17  public function extractValue(\Magento\Framework\App\RequestInterface $request)
18  {
19  $value = $this->_getRequestValue($request);
20  return $this->_applyInputFilter($value);
21  }
22 
28  public function validateValue($value)
29  {
30  $errors = [];
31  $attribute = $this->getAttribute();
32  $label = $attribute->getStoreLabel();
33 
34  if ($value === false) {
35  // try to load original value and validate it
37  }
38 
39  if ($attribute->isRequired() && empty($value)) {
40  $errors[] = __('"%1" is a required value.', $label);
41  }
42 
43  if (!$errors && !$attribute->isRequired() && empty($value)) {
44  return true;
45  }
46 
48  if ($result !== true) {
49  $errors = array_merge($errors, $result);
50  }
51 
52  //range validation
53  $validateRules = $attribute->getValidationRules();
54 
55  $minDateValue = ArrayObjectSearch::getArrayElementByName(
56  $validateRules,
57  'date_range_min'
58  );
59 
60  $maxDateValue = ArrayObjectSearch::getArrayElementByName(
61  $validateRules,
62  'date_range_max'
63  );
64 
65  if ($minDateValue !== null && strtotime($value) < $minDateValue
66  || $maxDateValue !== null && strtotime($value) > $maxDateValue
67  ) {
68  if ($minDateValue !== null && $maxDateValue !== null) {
69  $errors[] = __(
70  'Please enter a valid date between %1 and %2 at %3.',
71  date('d/m/Y', $minDateValue),
72  date('d/m/Y', $maxDateValue),
73  $label
74  );
75  } elseif ($minDateValue !== null) {
76  $errors[] = __(
77  'Please enter a valid date equal to or greater than %1 at %2.',
78  date('d/m/Y', $minDateValue),
79  $label
80  );
81  } elseif ($maxDateValue !== null) {
82  $errors[] = __(
83  'Please enter a valid date less than or equal to %1 at %2.',
84  date('d/m/Y', $maxDateValue),
85  $label
86  );
87  }
88  }
89 
90  if (count($errors) == 0) {
91  return true;
92  }
93 
94  return $errors;
95  }
96 
100  public function compactValue($value)
101  {
102  return $value;
103  }
104 
108  public function restoreValue($value)
109  {
110  return $this->compactValue($value);
111  }
112 
117  {
119  if ($value) {
120  switch ($format) {
121  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT:
122  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_HTML:
123  case \Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_PDF:
124  $this->_dateFilterFormat(\IntlDateFormatter::MEDIUM);
125  break;
126  }
127  $value = $this->_applyOutputFilter($value);
128  }
129 
130  $this->_dateFilterFormat(\IntlDateFormatter::SHORT);
131 
132  return $value;
133  }
134 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
_getRequestValue(\Magento\Framework\App\RequestInterface $request)
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
outputValue($format=\Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
Definition: Date.php:116
extractValue(\Magento\Framework\App\RequestInterface $request)
Definition: Date.php:17
$errors
Definition: overview.phtml:9