我想只获取已触发的消息,但我收到了所有已注册的消息。
var ticket_arr = '[{"Id":"a874062a-3f1e-4f91-a9d7-334aedd2e79f","Name":"Tesr","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"19f3722f-5f19-430a-bf8a-699c6841aec2","Name":"Go define this test projecct","StartDate":"2015-04-21","EndDate":"2015-04-29","Duration":"8","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5576edbf-3feb-4204-b488-0d0a35688538","Name":"Mike is getting a lesson in cool","StartDate":"2015-04-08","EndDate":"2015-04-10","Duration":"2","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5bf5d77c-73b1-480b-8758-2e822db73964","Name":"Test ticket","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false}]';
function sortArray(arr, name){
arr.sort(function(a,b)
{
if (new Date(a[name]) > new Date(b[name]) )
return 1;
else if (new Date(a[name]) < new Date(b[name]) )
return -1;
else
return 0;
});
}
function getMinMaxDate(arr, name, type)
{
if(arr.length)
{
sortArray(arr, name);
console.log(arr);
console.log(name);
if(type == "Min" && arr.length)
{
console.log(arr[0][name]);
return arr[0][name];
}
if(type == "Max" && arr.length)
{
return arr[arr.length-1][name];
}
}
else
return '';
}
稍后在控制器中:
$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.'],
),
),
));
如果我尝试上传5MB大小的图片,我会收到所有消息:
if(!$filter->isValid()){
var_dump($filter->getMessages());
}
但仅预期&#34;图像的最大文件大小为2MB&#34;。
有没有办法只返回触发的消息? 这应该是getMessages()方法的默认行为吗?
答案 0 :(得分:1)
可能的解决方案是使用Validator Chains。
在某些情况下,如果验证程序的验证过程失败,那么验证器就会破坏链条。
Zend\Validator\ValidatorChain
支持使用attach()
方法的第二个参数的此类用例。通过将$breakChainOnFailure
设置为TRUE
,添加的验证器将在发生故障时中断链执行,从而避免运行任何其他确定为不必要或不适合该情况的验证。
这样,验证在第一次失败时停止,您将只有验证失败的消息。您还可以设置优先级,以便按特定顺序应用验证器。文档中给出的这个示例使用方法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,
),
),
));