ZF2 文件验证器 return 所有消息但只需要触发
ZF2 File validators return all messages but need only triggered
我只想收到触发消息,但我收到了所有注册消息。
$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'
)
);
但预计只有 "Maximum file size for image is 2MB"。
有什么方法可以return只触发消息吗?
这应该是 getMessages() 方法的默认行为吗?
一个可能的解决方案是使用 Validator Chains。
In some cases it makes sense to have a validator break the chain if its validation process fails. Zend\Validator\ValidatorChain
supports such use cases with the second parameter to the attach()
method. By setting $breakChainOnFailure
to TRUE
, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation.
这样,验证会在第一次失败时停止,您只会在验证失败时收到消息。您还可以设置优先级,以便您的验证器将按特定顺序应用。文档中给出的这个示例使用方法 attach
。这不是您真正需要的。
在您的情况下,您可以在验证器规范中使用 break_chain_on_failure
键并将值设置为 true。像这样:
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
'break_chain_on_failure' => true,
),
),
));
我只想收到触发消息,但我收到了所有注册消息。
$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'
)
);
但预计只有 "Maximum file size for image is 2MB"。
有什么方法可以return只触发消息吗? 这应该是 getMessages() 方法的默认行为吗?
一个可能的解决方案是使用 Validator Chains。
In some cases it makes sense to have a validator break the chain if its validation process fails.
Zend\Validator\ValidatorChain
supports such use cases with the second parameter to theattach()
method. By setting$breakChainOnFailure
toTRUE
, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation.
这样,验证会在第一次失败时停止,您只会在验证失败时收到消息。您还可以设置优先级,以便您的验证器将按特定顺序应用。文档中给出的这个示例使用方法 attach
。这不是您真正需要的。
在您的情况下,您可以在验证器规范中使用 break_chain_on_failure
键并将值设置为 true。像这样:
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
'break_chain_on_failure' => true,
),
),
));