/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = $this->getUserValue(); $dateValid = true; if ($this->_dateExists()) { if ($this->useCalendar()) { $dateValid = isset($value['date']) && preg_match('/^\\d{1,4}.+\\d{1,4}.+\\d{1,4}$/', $value['date']); } else { $dateValid = isset($value['day']) && isset($value['month']) && isset($value['year']) && $value['day'] > 0 && $value['month'] > 0 && $value['year'] > 0; } } $timeValid = true; if ($this->_timeExists()) { $timeValid = isset($value['hour']) && isset($value['minute']) && is_numeric($value['hour']) && is_numeric($value['minute']); } $isValid = $dateValid && $timeValid; if ($isValid) { $this->setUserValue(['date' => isset($value['date']) ? $value['date'] : '', 'year' => isset($value['year']) ? intval($value['year']) : 0, 'month' => isset($value['month']) ? intval($value['month']) : 0, 'day' => isset($value['day']) ? intval($value['day']) : 0, 'hour' => isset($value['hour']) ? intval($value['hour']) : 0, 'minute' => isset($value['minute']) ? intval($value['minute']) : 0, 'day_part' => isset($value['day_part']) ? $value['day_part'] : '', 'date_internal' => isset($value['date_internal']) ? $value['date_internal'] : '']); } elseif (!$isValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); if (!$dateValid) { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify date required option(s).')); } elseif (!$timeValid) { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify time required option(s).')); } else { throw new \Magento\Framework\Exception\LocalizedException(__('Please specify product\'s required option(s).')); } } else { $this->setUserValue(null); } return $this; }
/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws LocalizedException */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = $this->getUserValue(); if (empty($value) && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); throw new LocalizedException(__('Please specify product\'s required option(s).')); } if (!$this->_isSingleSelection()) { $valuesCollection = $option->getOptionValuesByOptionId($value, $this->getProduct()->getStoreId())->load(); if ($valuesCollection->count() != count($value)) { $this->setIsValid(false); throw new LocalizedException(__('Please specify product\'s required option(s).')); } } return $this; }
/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = trim($this->getUserValue()); // Check requires option to have some value if (strlen($value) == 0 && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); throw new LocalizedException(__('Please specify product\'s required option(s).')); } // Check maximal length limit $maxCharacters = $option->getMaxCharacters(); if ($maxCharacters > 0 && $this->string->strlen($value) > $maxCharacters) { $this->setIsValid(false); throw new LocalizedException(__('The text is too long.')); } $this->setUserValue($value); return $this; }
/** * Validate user input for option * * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...) * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function validateUserValue($values) { parent::validateUserValue($values); $option = $this->getOption(); $value = $this->getUserValue(); $dateTime = \DateTime::createFromFormat(DateTime::DATETIME_PHP_FORMAT, $value); $dateValid = true; $lastErrors = \DateTime::getLastErrors(); if (!($dateTime && $lastErrors['error_count'] == 0)) { $dateValid = false; } if ($dateValid && $dateTime) { $this->setUserValue(['date' => $value, 'year' => $dateTime->format('Y'), 'month' => $dateTime->format('m'), 'day' => $dateTime->format('d'), 'hour' => $dateTime->format('H'), 'minute' => intval($dateTime->format('i')), 'day_part' => $dateTime->format('a'), 'date_internal' => '']); } elseif (!$dateValid && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) { $this->setIsValid(false); throw new \Magento\Framework\Exception\LocalizedException(__('Please specify product\'s required option(s).')); } else { $this->setUserValue(null); return $this; } return $this; }