0

那是我在 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 验证器?这是正确的方法吗?提前致谢。

4

1 回答 1

0

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

于 2013-02-18T09:12:36.487 回答