如何在表单中创建数组元素 - 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 has a special FileInput
class.
使用这个 class 很重要,因为它还会做其他重要的事情,例如 validation before filtering. There are also special filters like the 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);
检查 the documentation 以获取有关文件上传的更多信息。
或查看 the documentation here 如何制作上传表单
我想创建以下元素:
<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 has a special FileInput
class.
使用这个 class 很重要,因为它还会做其他重要的事情,例如 validation before filtering. There are also special filters like the 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);
检查 the documentation 以获取有关文件上传的更多信息。 或查看 the documentation here 如何制作上传表单