Exemple #1
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function validateValue($value)
 {
     $errors = [];
     $attribute = $this->getAttribute();
     $label = __($attribute->getStoreLabel());
     if ($value === false) {
         // try to load original value and validate it
         $value = $this->_value;
     }
     if ($attribute->isRequired() && empty($value) && $value !== '0') {
         $errors[] = __('"%1" is a required value.', $label);
     }
     if (!$errors && !$attribute->isRequired() && empty($value)) {
         return true;
     }
     // validate length
     $length = $this->_string->strlen(trim($value));
     $validateRules = $attribute->getValidationRules();
     $minTextLength = ArrayObjectSearch::getArrayElementByName($validateRules, 'min_text_length');
     if ($minTextLength !== null && $length < $minTextLength) {
         $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
     }
     $maxTextLength = ArrayObjectSearch::getArrayElementByName($validateRules, 'max_text_length');
     if ($maxTextLength !== null && $length > $maxTextLength) {
         $errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
     }
     $result = $this->_validateInputRule($value);
     if ($result !== true) {
         $errors = array_merge($errors, $result);
     }
     if (count($errors) == 0) {
         return true;
     }
     return $errors;
 }
 /**
  * Validate file by attribute validate rules
  * Return array of errors
  *
  * @param array $value
  * @return string[]
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _validateByRules($value)
 {
     $label = $value['name'];
     $rules = $this->getAttribute()->getValidationRules();
     $imageProp = @getimagesize($value['tmp_name']);
     if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) {
         return [__('"%1" is not a valid file.', $label)];
     }
     $allowImageTypes = [1 => 'gif', 2 => 'jpg', 3 => 'png'];
     if (!isset($allowImageTypes[$imageProp[2]])) {
         return [__('"%1" is not a valid image format.', $label)];
     }
     // modify image name
     $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
     if ($extension != $allowImageTypes[$imageProp[2]]) {
         $value['name'] = pathinfo($value['name'], PATHINFO_FILENAME) . '.' . $allowImageTypes[$imageProp[2]];
     }
     $maxFileSize = ArrayObjectSearch::getArrayElementByName($rules, 'max_file_size');
     $errors = [];
     if ($maxFileSize !== null) {
         $size = $value['size'];
         if ($maxFileSize < $size) {
             $errors[] = __('"%1" exceeds the allowed file size.', $label);
         }
     }
     $maxImageWidth = ArrayObjectSearch::getArrayElementByName($rules, 'max_image_width');
     if ($maxImageWidth !== null) {
         if ($maxImageWidth < $imageProp[0]) {
             $r = $maxImageWidth;
             $errors[] = __('"%1" width exceeds allowed value of %2 px.', $label, $r);
         }
     }
     $maxImageHeight = ArrayObjectSearch::getArrayElementByName($rules, 'max_image_height');
     if ($maxImageHeight !== null) {
         if ($maxImageHeight < $imageProp[1]) {
             $r = $maxImageHeight;
             $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r);
         }
     }
     return $errors;
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function validateValue($value)
 {
     $errors = [];
     $attribute = $this->getAttribute();
     $label = $attribute->getStoreLabel();
     if ($value === false) {
         // try to load original value and validate it
         $value = $this->_value;
     }
     if ($attribute->isRequired() && empty($value)) {
         $errors[] = __('"%1" is a required value.', $label);
     }
     if (!$errors && !$attribute->isRequired() && empty($value)) {
         return true;
     }
     $result = $this->_validateInputRule($value);
     if ($result !== true) {
         $errors = array_merge($errors, $result);
     }
     //range validation
     $validateRules = $attribute->getValidationRules();
     $minDateValue = ArrayObjectSearch::getArrayElementByName($validateRules, 'date_range_min');
     $maxDateValue = ArrayObjectSearch::getArrayElementByName($validateRules, 'date_range_max');
     if ($minDateValue !== null && strtotime($value) < $minDateValue || $maxDateValue !== null && strtotime($value) > $maxDateValue) {
         if ($minDateValue !== null && $maxDateValue !== null) {
             $errors[] = __('Please enter a valid date between %1 and %2 at %3.', date('d/m/Y', $minDateValue), date('d/m/Y', $maxDateValue), $label);
         } elseif ($minDateValue !== null) {
             $errors[] = __('Please enter a valid date equal to or greater than %1 at %2.', date('d/m/Y', $minDateValue), $label);
         } elseif ($maxDateValue !== null) {
             $errors[] = __('Please enter a valid date less than or equal to %1 at %2.', date('d/m/Y', $maxDateValue), $label);
         }
     }
     if (count($errors) == 0) {
         return true;
     }
     return $errors;
 }
Exemple #4
0
 /**
  * Return maximal date range value
  *
  * @return string|null
  */
 public function getMaxDateRange()
 {
     $dob = $this->_getAttribute('dob');
     if ($dob !== null) {
         $rules = $this->_getAttribute('dob')->getValidationRules();
         $maxDateValue = ArrayObjectSearch::getArrayElementByName($rules, self::MAX_DATE_RANGE_KEY);
         if ($maxDateValue !== null) {
             return date("Y/m/d", $maxDateValue);
         }
     }
     return null;
 }
 /**
  * Validate value by attribute input validation rule
  *
  * @param string $value
  * @return array|true
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _validateInputRule($value)
 {
     // skip validate empty value
     if (empty($value)) {
         return true;
     }
     $label = $this->getAttribute()->getStoreLabel();
     $validateRules = $this->getAttribute()->getValidationRules();
     $inputValidation = ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation');
     if (!is_null($inputValidation)) {
         switch ($inputValidation) {
             case 'alphanumeric':
                 $validator = new \Zend_Validate_Alnum(true);
                 $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alnum::INVALID);
                 $validator->setMessage(__('"%1" contains non-alphabetic or non-numeric characters.', $label), \Zend_Validate_Alnum::NOT_ALNUM);
                 $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alnum::STRING_EMPTY);
                 if (!$validator->isValid($value)) {
                     return $validator->getMessages();
                 }
                 break;
             case 'numeric':
                 $validator = new \Zend_Validate_Digits();
                 $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Digits::INVALID);
                 $validator->setMessage(__('"%1" contains non-numeric characters.', $label), \Zend_Validate_Digits::NOT_DIGITS);
                 $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Digits::STRING_EMPTY);
                 if (!$validator->isValid($value)) {
                     return $validator->getMessages();
                 }
                 break;
             case 'alpha':
                 $validator = new \Zend_Validate_Alpha(true);
                 $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Alpha::INVALID);
                 $validator->setMessage(__('"%1" contains non-alphabetic characters.', $label), \Zend_Validate_Alpha::NOT_ALPHA);
                 $validator->setMessage(__('"%1" is an empty string.', $label), \Zend_Validate_Alpha::STRING_EMPTY);
                 if (!$validator->isValid($value)) {
                     return $validator->getMessages();
                 }
                 break;
             case 'email':
                 /**
                 __("'%value%' appears to be a DNS hostname but the given punycode notation cannot be decoded")
                 __("Invalid type given. String expected")
                 __("'%value%' appears to be a DNS hostname but contains a dash in an invalid position")
                 __("'%value%' does not match the expected structure for a DNS hostname")
                 __("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'")
                 __("'%value%' does not appear to be a valid local network name")
                 __("'%value%' does not appear to be a valid URI hostname")
                 __("'%value%' appears to be an IP address, but IP addresses are not allowed")
                 __("'%value%' appears to be a local network name but local network names are not allowed")
                 __("'%value%' appears to be a DNS hostname but cannot extract TLD part")
                 __("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
                 */
                 $validator = new \Zend_Validate_EmailAddress();
                 $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_EmailAddress::INVALID);
                 $validator->setMessage(__('"%1" is not a valid email address.', $label), \Zend_Validate_EmailAddress::INVALID_FORMAT);
                 $validator->setMessage(__('"%1" is not a valid hostname.', $label), \Zend_Validate_EmailAddress::INVALID_HOSTNAME);
                 $validator->setMessage(__('"%1" is not a valid hostname.', $label), \Zend_Validate_EmailAddress::INVALID_MX_RECORD);
                 $validator->setMessage(__('"%1" is not a valid hostname.', $label), \Zend_Validate_EmailAddress::INVALID_MX_RECORD);
                 $validator->setMessage(__('"%1" is not a valid email address.', $label), \Zend_Validate_EmailAddress::DOT_ATOM);
                 $validator->setMessage(__('"%1" is not a valid email address.', $label), \Zend_Validate_EmailAddress::QUOTED_STRING);
                 $validator->setMessage(__('"%1" is not a valid email address.', $label), \Zend_Validate_EmailAddress::INVALID_LOCAL_PART);
                 $validator->setMessage(__('"%1" uses too many characters.', $label), \Zend_Validate_EmailAddress::LENGTH_EXCEEDED);
                 $validator->setMessage(__("'%value%' looks like an IP address, which is not an acceptable format."), \Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED);
                 $validator->setMessage(__("'%value%' looks like a DNS hostname but we cannot match the TLD against known list."), \Zend_Validate_Hostname::UNKNOWN_TLD);
                 $validator->setMessage(__("'%value%' looks like a DNS hostname but contains a dash in an invalid position."), \Zend_Validate_Hostname::INVALID_DASH);
                 $validator->setMessage(__("'%value%' looks like a DNS hostname but we cannot match it against the hostname schema for TLD '%tld%'."), \Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA);
                 $validator->setMessage(__("'%value%' looks like a DNS hostname but cannot extract TLD part."), \Zend_Validate_Hostname::UNDECIPHERABLE_TLD);
                 $validator->setMessage(__("'%value%' does not look like a valid local network name."), \Zend_Validate_Hostname::INVALID_LOCAL_NAME);
                 $validator->setMessage(__("'%value%' looks like a local network name, which is not an acceptable format."), \Zend_Validate_Hostname::LOCAL_NAME_NOT_ALLOWED);
                 $validator->setMessage(__("'%value%' appears to be a DNS hostname, but the given punycode notation cannot be decoded."), \Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE);
                 if (!$validator->isValid($value)) {
                     return array_unique($validator->getMessages());
                 }
                 break;
             case 'url':
                 $parsedUrl = parse_url($value);
                 if ($parsedUrl === false || empty($parsedUrl['scheme']) || empty($parsedUrl['host'])) {
                     return [__('"%1" is not a valid URL.', $label)];
                 }
                 $validator = new \Zend_Validate_Hostname();
                 if (!$validator->isValid($parsedUrl['host'])) {
                     return [__('"%1" is not a valid URL.', $label)];
                 }
                 break;
             case 'date':
                 $validator = new \Zend_Validate_Date(\Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT);
                 $validator->setMessage(__('"%1" invalid type entered.', $label), \Zend_Validate_Date::INVALID);
                 $validator->setMessage(__('"%1" is not a valid date.', $label), \Zend_Validate_Date::INVALID_DATE);
                 $validator->setMessage(__('"%1" does not fit the entered date format.', $label), \Zend_Validate_Date::FALSEFORMAT);
                 if (!$validator->isValid($value)) {
                     return array_unique($validator->getMessages());
                 }
                 break;
         }
     }
     return true;
 }
Exemple #6
0
 /**
  * Validate file by attribute validate rules
  * Return array of errors
  *
  * @param array $value
  * @return string[]
  */
 protected function _validateByRules($value)
 {
     $label = $value['name'];
     $rules = $this->getAttribute()->getValidationRules();
     $extension = pathinfo($value['name'], PATHINFO_EXTENSION);
     $fileExtensions = ArrayObjectSearch::getArrayElementByName($rules, 'file_extensions');
     if ($fileExtensions !== null) {
         $extensions = explode(',', $fileExtensions);
         $extensions = array_map('trim', $extensions);
         if (!in_array($extension, $extensions)) {
             return [__('"%1" is not a valid file extension.', $extension)];
         }
     }
     /**
      * Check protected file extension
      */
     if (!$this->_fileValidator->isValid($extension)) {
         return $this->_fileValidator->getMessages();
     }
     if (!$this->_isUploadedFile($value['tmp_name'])) {
         return [__('"%1" is not a valid file.', $label)];
     }
     $maxFileSize = ArrayObjectSearch::getArrayElementByName($rules, 'max_file_size');
     if ($maxFileSize !== null) {
         $size = $value['size'];
         if ($maxFileSize < $size) {
             return [__('"%1" exceeds the allowed file size.', $label)];
         }
     }
     return [];
 }