Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Text.php
Go to the documentation of this file.
1 <?php
10 
13 
14 class Text extends AbstractData
15 {
19  protected $_string;
20 
31  public function __construct(
32  \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
35  \Magento\Framework\Locale\ResolverInterface $localeResolver,
36  $value,
38  $isAjax,
39  \Magento\Framework\Stdlib\StringUtils $stringHelper
40  ) {
41  parent::__construct($localeDate, $logger, $attribute, $localeResolver, $value, $entityTypeCode, $isAjax);
42  $this->_string = $stringHelper;
43  }
44 
48  public function extractValue(\Magento\Framework\App\RequestInterface $request)
49  {
50  return $this->_applyInputFilter($this->_getRequestValue($request));
51  }
52 
58  public function validateValue($value)
59  {
60  $errors = [];
61  $attribute = $this->getAttribute();
62  $label = __($attribute->getStoreLabel());
63 
64  if ($value === false) {
65  // try to load original value and validate it
67  }
68 
69  if ($attribute->isRequired() && empty($value) && $value !== '0') {
70  $errors[] = __('"%1" is a required value.', $label);
71  }
72 
73  if (!$errors && !$attribute->isRequired() && empty($value)) {
74  return true;
75  }
76 
77  $errors = $this->validateLength($value, $attribute, $errors);
78 
80  if ($result !== true) {
81  $errors = array_merge($errors, $result);
82  }
83  if (count($errors) == 0) {
84  return true;
85  }
86 
87  return $errors;
88  }
89 
93  public function compactValue($value)
94  {
95  return $value;
96  }
97 
101  public function restoreValue($value)
102  {
103  return $this->compactValue($value);
104  }
105 
110  {
111  return $this->_applyOutputFilter($this->_value);
112  }
113 
122  private function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
123  {
124  // validate length
125  $label = __($attribute->getStoreLabel());
126 
127  $length = $this->_string->strlen(trim($value));
128 
129  $validateRules = $attribute->getValidationRules();
130 
131  if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
132  $minTextLength = ArrayObjectSearch::getArrayElementByName(
133  $validateRules,
134  'min_text_length'
135  );
136  if ($minTextLength !== null && $length < $minTextLength) {
137  $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
138  }
139 
140  $maxTextLength = ArrayObjectSearch::getArrayElementByName(
141  $validateRules,
142  'max_text_length'
143  );
144  if ($maxTextLength !== null && $length > $maxTextLength) {
145  $errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
146  }
147  }
148 
149  return $errors;
150  }
151 }
__()
Definition: __.php:13
$logger
_getRequestValue(\Magento\Framework\App\RequestInterface $request)
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
extractValue(\Magento\Framework\App\RequestInterface $request)
Definition: Text.php:48
__construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Psr\Log\LoggerInterface $logger, AttributeMetadataInterface $attribute, \Magento\Framework\Locale\ResolverInterface $localeResolver, $value, $entityTypeCode, $isAjax, \Magento\Framework\Stdlib\StringUtils $stringHelper)
Definition: Text.php:31
$errors
Definition: overview.phtml:9
outputValue($format=\Magento\Customer\Model\Metadata\ElementFactory::OUTPUT_FORMAT_TEXT)
Definition: Text.php:109