这是我在Object\model:中的模型SiteDesign.php
namespace ObjectModel;
use ZendValidatorStringLength;
use ZendValidatorNotEmpty;
use ZendValidatorFileSize;
use ZendInputFilterFactory as InputFactory;
use ZendInputFilterInputFilter;
use ZendInputFilterInputFilterAwareInterface;
use ZendInputFilterInputFilterInterface;
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验证器?这是正确的方法吗?提前谢谢。
对于ZendValidatorValidatorPluginManager
(负责按名称实例化验证器),正确的名称是`"FileSize"(其中有太多"s")。