2016-10-06 9 views
-1

Abstract Validators에 대해 질문이 있습니다. Mb Rostami found here의 솔루션을 구현하려고했습니다.ValidatorPluginManager get이 모델 입력 필터에서 작업을 수행하지 못했습니다.

입니다 오류가 나는 얻을 :

젠드 \ 검사기 \ ValidatorPluginManager :: 가져 오거나 모든 내가 필요

응용 프로그램 \ 검사기 \ 파일 \ 이미지에 대한 인스턴스를 만들 수 없습니다 얻을 어떻게 든 클래스를 모델에 삽입하는 것입니다. Application\Validators\File\Image은 무엇입니까?

어떻게이 오류를 해결할 수 있습니까? 가장 쉬운 해결책은 발리 데이터 클래스를 호출 가능 모듈로 추가하는 것입니다. 모델 클래스

입력 필터 :

public function getInputFilter() 
{ 
    if (!$this->inputFilter) { 
     $inputFilter = new InputFilter(); 

     $inputFilter->add(array(
      'name' => 'eid', 
      'required' => true, 
      'filters' => array(
       array('name' => 'Int'), 
      ) 
     )); 

     $newFileName = sha1(time(), true); 
     $inputFilter->add(
      array(
       'name' => 'ImageValidator', 
       'required' => true, 
       'validators' => array(
        array(
         'name' => '\Application\Validators\File\Image', 
         'options' => array(
          'minSize' => '64', 
          'maxSize' => '5120', 
          'newFileName' => $newFileName, 
          'uploadPath' => './data/' 
         ), 
        ), 

       ) 
      ) 
     ); 

     $this->inputFilter = $inputFilter; 
    } 

    return $this->inputFilter; 
} 

검사기 클래스 :

<?php 

namespace Application\Validators\File; 

use Zend\Validator\File\Extension; 
use Zend\File\Transfer\Adapter\Http; 
use Zend\Validator\File\FilesSize; 
use Zend\Filter\File\Rename; 
use Zend\Validator\File\MimeType; 
use Zend\Validator\AbstractValidator; 

class Image extends AbstractValidator 
{ 
const FILE_EXTENSION_ERROR = 'invalidFileExtention'; 
const FILE_NAME_ERROR = 'invalidFileName'; 
const FILE_INVALID = 'invalidFile'; 
const FALSE_EXTENSION = 'fileExtensionFalse'; 
const NOT_FOUND = 'fileExtensionNotFound'; 
const TOO_BIG = 'fileFilesSizeTooBig'; 
const TOO_SMALL = 'fileFilesSizeTooSmall'; 
const NOT_READABLE = 'fileFilesSizeNotReadable'; 


public $minSize = 64; //KB 
public $maxSize = 1024; //KB 
public $overwrite = true; 
public $newFileName = null; 
public $uploadPath = './data/'; 
public $extensions = array('jpg', 'png', 'gif', 'jpeg'); 
public $mimeTypes = array(
    'image/gif', 
    'image/jpg', 
    'image/png', 
); 

protected $messageTemplates = array(
    self::FILE_EXTENSION_ERROR => "File extension is not correct", 
    self::FILE_NAME_ERROR => "File name is not correct", 
    self::FILE_INVALID => "File is not valid", 
    self::FALSE_EXTENSION => "File has an incorrect extension", 
    self::NOT_FOUND => "File is not readable or does not exist", 
    self::TOO_BIG => "All files in sum should have a maximum size of '%max%' but '%size%' were detected", 
    self::TOO_SMALL => "All files in sum should have a minimum size of '%min%' but '%size%' were detected", 
    self::NOT_READABLE => "One or more files can not be read", 
); 

protected $fileAdapter; 

protected $validators; 

protected $filters; 

public function __construct($options) 
{ 
    $this->fileAdapter = new Http(); 
    parent::__construct($options); 
} 

public function isValid($fileInput) 
{ 
    $options = $this->getOptions(); 
    $extensions = $this->extensions; 
    $minSize = $this->minSize; 
    $maxSize = $this->maxSize; 
    $newFileName = $this->newFileName; 
    $uploadPath = $this->uploadPath; 
    $overwrite = $this->overwrite; 
    if (array_key_exists('extensions', $options)) { 
     $extensions = $options['extensions']; 
    } 
    if (array_key_exists('minSize', $options)) { 
     $minSize = $options['minSize']; 
    } 
    if (array_key_exists('maxSize', $options)) { 
     $maxSize = $options['maxSize']; 
    } 
    if (array_key_exists('newFileName', $options)) { 
     $newFileName = $options['newFileName']; 
    } 
    if (array_key_exists('uploadPath', $options)) { 
     $uploadPath = $options['uploadPath']; 
    } 
    if (array_key_exists('overwrite', $options)) { 
     $overwrite = $options['overwrite']; 
    } 
    $fileName = $fileInput['name']; 
    $fileSizeOptions = null; 
    if ($minSize) { 
     $fileSizeOptions['min'] = $minSize * 1024; 
    } 
    if ($maxSize) { 
     $fileSizeOptions['max'] = $maxSize * 1024; 
    } 
    if ($fileSizeOptions) { 
     $this->validators[] = new FilesSize($fileSizeOptions); 
    } 
    $this->validators[] = new Extension(array('extension' => $extensions)); 
    if (!preg_match('/^[a-z0-9-_]+[a-z0-9-_\.]+$/i', $fileName)) { 
     $this->error(self::FILE_NAME_ERROR); 
     return false; 
    } 

    $extension = pathinfo($fileName, PATHINFO_EXTENSION); 
    if (!in_array($extension, $extensions)) { 
     $this->error(self::FILE_EXTENSION_ERROR); 
     return false; 
    } 
    if ($newFileName) { 
     $destination = $newFileName . ".$extension"; 
     if (!preg_match('/^[a-z0-9-_]+[a-z0-9-_\.]+$/i', $destination)) { 
      $this->error(self::FILE_NAME_ERROR); 
      return false; 
     } 
    } else { 
     $destination = $fileName; 
    } 
    $renameOptions['target'] = $uploadPath . $destination; 
    $renameOptions['overwrite'] = $overwrite; 
    $this->filters[] = new Rename($renameOptions); 
    $this->fileAdapter->setFilters($this->filters); 
    $this->fileAdapter->setValidators($this->validators); 
    if ($this->fileAdapter->isValid()) { 
     $this->fileAdapter->receive(); 
     return true; 
    } else { 
     $messages = $this->fileAdapter->getMessages(); 
     if ($messages) { 
      $this->setMessages($messages); 
      foreach ($messages as $key => $value) { 
       $this->error($key); 
      } 
     } else { 
      $this->error(self::FILE_INVALID); 
     } 
     return false; 
    } 
} 
} 
+0

수정 됨 : 분명히 폴더 구조가 잘못 사용되었습니다. 파일 시스템의 \ module \ Application \ src \ Application \ Validators \ File에 있어야합니다. – Floris

+0

안녕하세요. Floris. [공동으로 편집 한 사이트] (https://stackoverflow.com/help/editing)입니다. 따라서 사용자가 말한 내용의 의미를 변경하지 않는 한 사용자는 기꺼이 수정을 기꺼이 수행해야합니다. 감사! – halfer

+0

질문에 대한 답변이 있다면 아래에 확장 된 답변을 추가 하시겠습니까? – halfer

답변

0

용액 적어도 제 경우 단순. 위에서 언급 한 것처럼 폴더 구조를 확인하십시오. 젠드 프레임 워크는 프로젝트 파일을 구조화하는 자체 방식이므로 파일 경로가 일치해야합니다.