我只想获取触发消息,但我正在获取所有已注册的消息。
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
),
array(
'name' => '\Zend\Validator\File\Extension',
'options' => ['extension' => 'png,jpg,jpeg', 'message' => 'Image extension has to be png,jpg or jpeg.'],
),
array(
'name' => '\Zend\Validator\File\Size',
'options' => ['max' => '2MB', 'message' => 'Maximum file size for image is 2MB.'],
),
),
));
稍后在控制器中:
if(!$filter->isValid()){
var_dump($filter->getMessages());
}
如果我尝试上传 5MB 大小的图像,我会收到所有消息:
array(
'image' => array(
'fileIsImageNotReadable' => 'File has to be valid image'
'fileExtensionNotFound' => 'Image extension has to be png,jpg or jpeg'
'fileSizeNotFound' => 'Maximum file size for image is 2MB'
)
);
但只期望“图像的最大文件大小为 2MB”。
有没有办法只返回触发消息?这应该是 getMessages() 方法的默认行为吗?