使用工厂的Model中的FilesSize验证器

时间:2013-02-18 00:55:25

标签: file validation frameworks zend-framework2

这是我在Object \ Model中的模型SiteDesign.php:

namespace Object\Model;

use Zend\Validator\StringLength;
use Zend\Validator\NotEmpty;
use Zend\Validator\File\Size;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class SiteDesign {

public $logo_filename;
public $background_image;
public $background_color;

public function exchangeArray($data) {
    $this->logo_filename = (isset($data['logo_filename'])) ? $data['logo_filename'] : null;
    $this->background_image = (isset($data['background_image'])) ? $data['background_image'] : null;
    $this->background_color = (isset($data['background_color'])) ? $data['background_color'] : null;
}

public function setInputFilter(InputFilterInterface $inputFilter) {
    throw new \Exception('Not used');
}

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

        $factory = new InputFactory();

        $inputFilter->add($factory->createInput(array(
                    'name' => 'logo_filename',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'FilesSize',
                            'options' => array(
                                'max' => 4 * 1024,
                                'messages' => array(
                                    Size::TOO_BIG => 'Размерът на логото не може да надвишава 5MB.',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'background_image',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'FilesSize',
                            'options' => array(
                                'max' => 4 * 1024,
                                'messages' => array(
                                    Size::TOO_BIG => 'Размерът на изображението за фон не може да надвишава 5MB.',
                                ),
                            ),
                        ),
                    ),
                )));

        $inputFilter->add($factory->createInput(array(
                    'name' => 'background_color',
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                    ),
                )));

        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}

}

但是有一条错误消息:

Zend \ ServiceManager \ ServiceManager :: get无法为FilesSize获取或创建实例

那么是否可以使用工厂直接在模型中定义FilesSize验证器?这是正确的方法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

Zend\Validator\ValidatorPluginManager开始(它负责按名称实例化验证器)正确的名称是“FileSize”(你那里有太多的“s”)。