示例#1
0
 /**
  * @inheritDoc IFieldType::validate()
  *
  * @param array $value
  *
  * @return true|string|array
  */
 public function validate($value)
 {
     $errors = parent::validate($value);
     if (!is_array($errors)) {
         $errors = array();
     }
     $settings = $this->getSettings();
     // Check if this field restricts files and if files are passed at all.
     if (isset($settings->restrictFiles) && !empty($settings->restrictFiles) && !empty($settings->allowedKinds) && is_array($value) && !empty($value)) {
         $allowedExtensions = static::_getAllowedExtensions($settings->allowedKinds);
         foreach ($value as $fileId) {
             $file = craft()->assets->getFileById($fileId);
             if ($file && !in_array(mb_strtolower(IOHelper::getExtension($file->filename)), $allowedExtensions)) {
                 $errors[] = Craft::t('"{filename}" is not allowed in this field.', array('filename' => $file->filename));
             }
         }
     }
     foreach ($this->_failedFiles as $file) {
         $errors[] = Craft::t('"{filename}" is not allowed in this field.', array('filename' => $file));
     }
     if ($errors) {
         return $errors;
     } else {
         return true;
     }
 }