/** * {@inheritdoc} */ public function validateValue($value) { $errors = array(); $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 (!is_null($minTextLength) && $length < $minTextLength) { $errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength); } $maxTextLength = ArrayObjectSearch::getArrayElementByName($validateRules, 'max_text_length'); if (!is_null($maxTextLength) && $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[] */ protected function _validateByRules($value) { $label = $value['name']; $rules = $this->getAttribute()->getValidationRules(); $imageProp = @getimagesize($value['tmp_name']); if (!$this->_isUploadedFile($value['tmp_name']) || !$imageProp) { return array(__('"%1" is not a valid file.', $label)); } $allowImageTypes = array(1 => 'gif', 2 => 'jpg', 3 => 'png'); if (!isset($allowImageTypes[$imageProp[2]])) { return array(__('"%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 = array(); if (!is_null($maxFileSize)) { $size = $value['size']; if ($maxFileSize < $size) { $errors[] = __('"%1" exceeds the allowed file size.', $label); } } $maxImageWidth = ArrayObjectSearch::getArrayElementByName($rules, 'max_image_width'); if (!is_null($maxImageWidth)) { 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 (!is_null($maxImageHeight)) { if ($maxImageHeight < $imageProp[1]) { $r = $maxImageHeight; $errors[] = __('"%1" height exceeds allowed value of %2 px.', $label, $r); } } return $errors; }
/** * {@inheritdoc} */ public function validateValue($value) { $errors = array(); $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 (!is_null($minDateValue) && strtotime($value) < $minDateValue || !is_null($maxDateValue) && strtotime($value) > $maxDateValue) { if (!is_null($minDateValue) && !is_null($maxDateValue)) { $errors[] = __('Please enter a valid date between %1 and %2 at %3.', date('d/m/Y', $minDateValue), date('d/m/Y', $maxDateValue), $label); } elseif (!is_null($minDateValue)) { $errors[] = __('Please enter a valid date equal to or greater than %1 at %2.', date('d/m/Y', $minDateValue), $label); } elseif (!is_null($maxDateValue)) { $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; }
/** * Validate value by attribute input validation rule * * @param string $value * @return array|true */ 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" exceeds the allowed length.', $label), \Zend_Validate_EmailAddress::LENGTH_EXCEEDED); $validator->setMessage(__("'%value%' appears to be an IP address, but IP addresses are not allowed."), \Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED); $validator->setMessage(__("'%value%' appears to be a DNS hostname but cannot match TLD against known list."), \Zend_Validate_Hostname::UNKNOWN_TLD); $validator->setMessage(__("'%value%' appears to be a DNS hostname but contains a dash in an invalid position."), \Zend_Validate_Hostname::INVALID_DASH); $validator->setMessage(__("'%value%' appears to be a DNS hostname but cannot match against hostname schema for TLD '%tld%'."), \Zend_Validate_Hostname::INVALID_HOSTNAME_SCHEMA); $validator->setMessage(__("'%value%' appears to be a DNS hostname but cannot extract TLD part."), \Zend_Validate_Hostname::UNDECIPHERABLE_TLD); $validator->setMessage(__("'%value%' does not appear to be a valid local network name."), \Zend_Validate_Hostname::INVALID_LOCAL_NAME); $validator->setMessage(__("'%value%' appears to be a local network name but local network names are not allowed."), \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 array(__('"%1" is not a valid URL.', $label)); } $validator = new \Zend_Validate_Hostname(); if (!$validator->isValid($parsedUrl['host'])) { return array(__('"%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; }
/** * 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 (!is_null($fileExtensions)) { $extensions = explode(',', $fileExtensions); $extensions = array_map('trim', $extensions); if (!in_array($extension, $extensions)) { return array(__('"%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 array(__('"%1" is not a valid file.', $label)); } $maxFileSize = ArrayObjectSearch::getArrayElementByName($rules, 'max_file_size'); if (!is_null($maxFileSize)) { $size = $value['size']; if ($maxFileSize < $size) { return array(__('"%1" exceeds the allowed file size.', $label)); } } return array(); }
/** * Return maximal date range value * * @return string|null */ public function getMaxDateRange() { $dob = $this->_getAttribute('dob'); if (!is_null($dob)) { $rules = $this->_getAttribute('dob')->getValidationRules(); $maxDateValue = ArrayObjectSearch::getArrayElementByName($rules, self::MAX_DATE_RANGE_KEY); if (!is_null($maxDateValue)) { return date("Y/m/d", $maxDateValue); } } return null; }