开发者

How can I customize the Zend_Validate_File_Upload::INI_SIZE error message?

开发者 https://www.devze.com 2023-03-10 19:53 出处:网络
I\'ve tried 3 methods with no change to the error message which outputs File \'image\' exceeds the defined ini size. My thinking is the message is captured somewhere and I haven\'t found out where. If

I've tried 3 methods with no change to the error message which outputs File 'image' exceeds the defined ini size. My thinking is the message is captured somewhere and I haven't found out where. If you have come across this let me know how you solved it -- much appreciated.

Method 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'
    )));

Method 2

$element->addErrorMessage(
    array(Zend_Val开发者_开发问答idate_File_Upload::INI_SIZE => 'File size is invalid'));

Method 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'),
));


$data = array(
    Zend_Validate_File_Size::TOO_BIG    => 'File size is invalid',
    Zend_Validate_File_Upload::INI_SIZE => 'File size is invalid'
);

$translator = new Zend_Translate('Array', $data, 'en_US');
$translator->getAdapter()->setLocale(new Zend_Locale('en_US'));
Zend_Validate_Abstract::setDefaultTranslator($translator);


I may have a solution. File upload are often hard to implement and customize, specially with zend, that's why I used a specific decorator to change the behavior and the rendering of my form. Thus you can do whatever you want regarding error message, a quick example :

Form example file :

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);

And the content of the file.phtml decorator file, where you can customize the rendering :

<?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>

Hope this help


Another possibility is:

$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);


I found the above methods awkward and hackish, so after debugging:

$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() gets the default adapter, which has Upload validator assigned to it by default. Now what needs to be done is overriding the default message using setMessage.

0

精彩评论

暂无评论...
验证码 换一张
取 消