[英]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.