Zend framework 如何自定义Zend_Validate_File_Upload::INI_SIZE错误消息?

Zend framework 如何自定义Zend_Validate_File_Upload::INI_SIZE错误消息?,zend-framework,zend-form,zend-form-element,Zend Framework,Zend Form,Zend Form Element,我尝试了3种方法,但没有更改错误消息,因为输出的文件“image”超过了定义的ini大小。我的想法是信息被捕获在某个地方,而我还没有发现在哪里。如果你遇到这个问题,请告诉我你是如何解决的——非常感谢 方法1 $element->addValidator('Size', false, '1MB', array('messages' => array( Zend_Validate_File_Size::TOO_BIG => 'File size is

我尝试了3种方法,但没有更改错误消息,因为输出的
文件“image”超过了定义的ini大小。我的想法是信息被捕获在某个地方,而我还没有发现在哪里。如果你遇到这个问题,请告诉我你是如何解决的——非常感谢

方法1

$element->addValidator('Size', false, '1MB', array('messages' => 
    array(
        Zend_Validate_File_Size::TOO_BIG    => 'File size is invalid',
        Zend_Validate_File_Upload::INI_SIZE => 'File size is invalid'
    )));
方法2

$element->addErrorMessage(
    array(Zend_Validate_File_Upload::INI_SIZE => 'File size is invalid'));
方法3

$element->addValidator('Callback', true,
    array(
        'callback' => function($value) {
            $validator = new Zend_Validate_File_Size();
            return $validator->isValid($value);
        },
        'messages' => array(
            Zend_Validate_Callback::INVALID_VALUE => 'File size is invalid'),
));

我可能有个解决办法。文件上传通常很难实现和自定义,特别是在zend中,这就是为什么我使用特定的装饰器来更改表单的行为和呈现。因此,您可以对错误消息执行任何操作,这是一个简单的示例:

表单示例文件:

private $fileDecorators = array(
    'File', array('ViewScript', array(
            'viewScript' => 'forms/file.phtml',
            'placement' => false)))

;

$uploadfile = new Zend_Form_Element_File('uploadfile', array(
                'disableLoadDefaultDecorators' => true,
                'decorators' => $this->fileDecorators,
                'description' => 'form_uploadfile_description',
                'label' => 'form_uploadfile_label',
                'required' => true,
                'destination' => '/tmp',
                'filters' => array(
                ),
                'validators' => array(
                    //array('StringLength', array(0, 256)),
                    array('Size', true, '1MB'),
                    array('Extension', true, 'jpg,png,gif'),
                    array('Count', true, 1)
                )
            ));
$this->addElement($uploadfile);
以及file.phtml decorator文件的内容,您可以在其中自定义渲染:

<?php
$type=explode('_',$this->element->getType());
$myclass = 'form_item ' . strtolower(end($type));

?>
<div class="<?php echo $myclass; ?>" id="field_<?php echo $this->element->getId(); ?>">
    <?php if (0 < strlen($this->element->getLabel())): ?>
        <?php echo $this->formLabel($this->element->getFullyQualifiedName(), $this->element->getLabel(),array('class'=>($this->element->isRequired())?' required':""));?>
    <?php endif; ?>
    <div class="float_100">
        <span class="file_wrapper">
            <?php echo $this->content; ?>
            <span class="button"><?=$this->translate('choose_an_image')?></span>
        </span>
    </div>

    <?php if (0 < strlen($this->element->getDescription())): ?>
        <div class="tooltip"><?php echo $this->element->getDescription(); ?></div>
    <?php endif; ?>

    <?php if (0 < strlen($this->element->getMessages())): ?>
        <div class="error">
        // Print your error here
        </div>
    <?php endif; ?>
</div>

是:


我发现上面的方法很笨拙,很粗糙,所以调试后:

$upload = new Zend_Form_Element_File('upload');

$upload->getTransferAdapter()->getValidator('Upload')->setMessage('File you are trying to upload is too big.', Zend_Validate_File_Upload::INI_SIZE);

getTransferAdapter()获取默认适配器,默认情况下为其分配了上载验证器。现在需要做的是使用setMessage覆盖默认消息。

非常有用,Jean,我会尝试一下,谢谢。希望这个问题有一个更内在的解决方案。我可能会应用您的方法并创建一个全新的表单元素。
$validator = new Zend_Validate_File_Upload();
$validator->setMessages(array(
    Zend_Validate_File_Upload::INI_SIZE => 'Die Datei ist größer als die maximal erlaubte Größe der ini-Konfiguration.',
    Zend_Validate_File_Upload::NO_FILE => 'Es muss eine Datei angegeben werden',
));
$element->addValidator($validator);
$upload = new Zend_Form_Element_File('upload');

$upload->getTransferAdapter()->getValidator('Upload')->setMessage('File you are trying to upload is too big.', Zend_Validate_File_Upload::INI_SIZE);