簡體   English   中英

如何在表單中創建數組元素-Zend Framework 2

[英]how to create an array element in form - zend framework 2

我想創建以下元素:

<input type="file" name="file[]">

我在myproject / module / Member / src / Member / Form / EditForm.php中嘗試了以下代碼:

$this->add(array(
            'name' => 'file',
            'type'  => 'file',
            'attributes' => array(                
                'class' => 'form-control col-md-7 col-xs-12',                
                'id' => 'file',                
            ),'options' => array(
                'multiple'=>TRUE
            ),
        ));

$this->add(array(
            'name' => 'file[]',
            'type'  => 'file',
            'attributes' => array(                
                'class' => 'form-control col-md-7 col-xs-12',                
                'id' => 'file',                
            ),'options' => array(
                'multiple'=>TRUE
            ),
        ));

但它不起作用。

對於文件上傳, Zend Framework 2具有一個特殊的FileInput

使用此類非常重要,因為它還會執行其他重要操作,例如在過濾之前進行驗證 還有一些特殊的過濾器,例如File\\RenameUpload ,可以為您重命名上載。

考慮到$this是您的InputFilter實例,代碼可能如下所示:

$this->add(array(
    'name' => 'file',
    'required' => true,
    'allow_empty' => false,
    'filters' => array(
        array(
            'name' => 'File\RenameUpload',
            'options' => array(
                'target' => 'upload',
                'randomize' => true,
                'overwrite' => true
            )
        )
    ),
    'validators' => array(
        array(
            'name' => 'FileSize',
            'options' => array(
                'max' => 10 * 1024 * 1024 // 10MB
            )
        )
    ),
    // IMPORTANT: this will make sure you get the `FileInput` class
    'type' => 'Zend\InputFilter\FileInput'
);

要將文件元素附加到表單:

// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
     ->setAttribute('id', 'file');
$this->add($file);

查看文檔以獲取有關文件上傳的更多信息。 在此處查看有關如何制作上傳表單的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM