Beispiel #1
0
 /**
  * Validate data
  * Return true or array of errors
  *
  * @param array|string $value
  * @return boolean|array
  */
 public function validateValue($value)
 {
     $errors = array();
     $attribute = $this->getAttribute();
     $label = $this->_translationHelper->__($attribute->getStoreLabel());
     if ($value === false) {
         // try to load original value and validate it
         $value = $this->getEntity()->getDataUsingMethod($attribute->getAttributeCode());
     }
     if ($attribute->getIsRequired() && empty($value) && $value !== '0') {
         $errors[] = $this->_translationHelper->__('"%s" is a required value.', $label);
     }
     if (!$errors && !$attribute->getIsRequired() && empty($value)) {
         return true;
     }
     // validate length
     $length = $this->_stringHelper->strlen(trim($value));
     $validateRules = $attribute->getValidateRules();
     if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
         $v = $validateRules['min_text_length'];
         $errors[] = $this->_translationHelper->__('"%s" length must be equal or greater than %s characters.', $label, $v);
     }
     if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
         $v = $validateRules['max_text_length'];
         $errors[] = $this->_translationHelper->__('"%s" length must be equal or less than %s characters.', $label, $v);
     }
     $result = $this->_validateInputRule($value);
     if ($result !== true) {
         $errors = array_merge($errors, $result);
     }
     if (count($errors) == 0) {
         return true;
     }
     return $errors;
 }
Beispiel #2
0
 /**
  * Check one attribute can be overridden in child
  *
  * @param string $attributeCode Attribute code
  * @param array $attributeParams Attribute params
  * @param array $rowData Row data
  * @param int $rowNumber
  * @return boolean
  */
 public function isAttributeValid($attributeCode, array $attributeParams, array $rowData, $rowNumber)
 {
     switch ($attributeParams['type']) {
         case 'varchar':
             $value = $this->_stringHelper->cleanString($rowData[$attributeCode]);
             $valid = $this->_stringHelper->strlen($value) < self::DB_MAX_VARCHAR_LENGTH;
             break;
         case 'decimal':
             $value = trim($rowData[$attributeCode]);
             $valid = (double) $value == $value && is_numeric($value);
             break;
         case 'select':
         case 'multiselect':
             $valid = isset($attributeParams['options'][strtolower($rowData[$attributeCode])]);
             break;
         case 'int':
             $value = trim($rowData[$attributeCode]);
             $valid = (int) $value == $value && is_numeric($value);
             break;
         case 'datetime':
             $value = trim($rowData[$attributeCode]);
             $valid = strtotime($value) !== false || preg_match('/^\\d{2}.\\d{2}.\\d{2,4}(?:\\s+\\d{1,2}.\\d{1,2}(?:.\\d{1,2})?)?$/', $value);
             break;
         case 'text':
             $value = $this->_stringHelper->cleanString($rowData[$attributeCode]);
             $valid = $this->_stringHelper->strlen($value) < self::DB_MAX_TEXT_LENGTH;
             break;
         default:
             $valid = true;
             break;
     }
     if (!$valid) {
         $this->addRowError($this->_helper('Mage_ImportExport_Helper_Data')->__("Invalid value for '%s'"), $rowNumber, $attributeCode);
     } elseif (!empty($attributeParams['is_unique'])) {
         if (isset($this->_uniqueAttributes[$attributeCode][$rowData[$attributeCode]])) {
             $this->addRowError($this->_helper('Mage_ImportExport_Helper_Data')->__("Duplicate Unique Attribute for '%s'"), $rowNumber, $attributeCode);
             return false;
         }
         $this->_uniqueAttributes[$attributeCode][$rowData[$attributeCode]] = true;
     }
     return (bool) $valid;
 }
Beispiel #3
0
 public function testStrpos()
 {
     $this->assertEquals(1, $this->_helper->strpos('123', 2));
 }
Beispiel #4
0
 public function cleanString($string)
 {
     $string = parent::cleanString($string);
     $string = preg_replace('/[^\\p{L}0-9\\-]/u', ' ', $string);
     return $string;
 }