2014-03-25 43 views
0

我已经查看了类似的答案,但找不到任何解决方案。Zend Framework 1.12 - Zend Form File元素触发错误的验证消息

我在我的一个表单中有一个文件元素。这个元素定义如下。

$file = $this->createElement('file', 'templateFile'); 
$file->setAttrib('id', 'templateFile') 
    ->setAttrib('class', 'form-control') 
    ->setAttrib('placeholder', 'File containing template headers') 
    ->setLabel('Template File <span class="glyphicons glyphicons-sheriffs_star requiredFlag"></span>') 
    ->setRequired(true) 
    ->addValidator('NotEmpty', false) 
    ->addValidator('Count', false, 1) 
    ->addValidator('Size', false, '1MB') 
    ->addValidator("Extension", false, array('csv', 'txt')) 
    ->addFilter('StringTrim'); 

$file->getValidator('NotEmpty')->setMessage('Please select a template file'); 
$file->getValidator('Count')->setMessage(' Please upload one file at a time'); 
$file->getValidator('Size')->setMessage('File exceeds the upload limit of 1 MB'); 
$file->getValidator('Extension')->setMessage('Invalid file extension. Only CSV and TXT files are allowed'); 

我对此元素使用自定义错误消息,这是什么导致问题。

当我提交表单而不选择文件而不是触发NotEmpty验证错误消息时,我会看到下面的消息。

File 'qhepstow_dblive.sql' exceeds the defined form size 

如何在未选择文件时触发以下错误消息并避免以上提到的一次?

未选择文件时应显示此错误消息。

Please select a template file 

感谢您的帮助。

回答

0

我设法解决了这个问题。希望它能帮助未来的人。

问题出在我将验证器添加到文件elemenet的顺序。

将序列更改为下方可解决问题。

$file = $this->createElement('file', 'templateFile'); 
     $file->setAttrib('id', 'templateFile') 
      ->setAttrib('class', 'form-control') 
      ->setAttrib('placeholder', 'File containing template headers') 
      ->setLabel('Template File <span class="glyphicons glyphicons-sheriffs_star requiredFlag"></span>') 
      ->setDestination($destination) 
      ->setRequired(true) 
      ->addValidator('Count', false, 1) 
      ->addValidator('NotEmpty', false) 
      ->addValidator('Size', false, '15MB') 
      ->addValidator("Extension", false, array('txt' ,'csv')) 
      ->addFilter('StringTrim');