0

I am trying to add custom error message to my file element validator. It is working for all the elements, BUT file. Kindly point out where am I going wrong. I know similar question has been asked before but I want to know what is the problem with this code?

    $file= new Zend_Form_Element_File('albumcover');
    $file->setAttrib('size',35)
    ->removeDecorator('label')
    ->removeDecorator('htmlTag');

    $file->setRequired(true)
    ->addValidator('Size',true,'1MB')         
    ->addValidator('Count',true,1)
    ->addValidator('IsImage',true,'jpg,jpeg,png'); 
    $file->addErrorMessage("Upload 'jpg,jpeg or png' file of less than 1MB in size");

It shows predefined errors not the error message that I have set

4

1 回答 1

0

您可能希望在控制器中执行此操作以检查元素是否有错误并打印特定的错误消息。

$form = My_Form_File();
...
if ($form->isValid()) {
...
} else {
    if ($form->getElement('albumcover')->hasErrors()) {
        $form->getElement('albumcover')->addError("Upload 'jpg,jpeg or png' file of less than 1MB in size");
    }
}

虽然我建议让用户在描述中知道

$file->setDescription("Upload 'jpg,jpeg or png' file of less than 1MB in size"); 
于 2013-02-05T13:00:57.803 回答