Jul*_*ien 6 php symfony-forms symfony twig
我正在努力在我的网站上上传.pdf文件.
我创建了一个实体,其中包含字符串中文件的名称.有实体映射:
<field name="pdf" type="string" column="pdf" length="200" nullable="true"/>
Run Code Online (Sandbox Code Playgroud)
我输入文件的表单:
$builder
->add('pdf', FileType::class, array('label' => 'Fiche de paie'))
/* other input */
;
Run Code Online (Sandbox Code Playgroud)
我的表格观点:
{{ form_start(form) }}
{{ form_row(form._token) }}
<div class="modal-body row">
<div class="col-md-6 col-sm-8">
/* other input */
{{ form_row(form.pdf) }}}
</div>
<div class="col-md-6 col-sm-8">
/* other input */
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" id="save">{% trans %}Save{% endtrans %}</button>
</div>
{{ form_end(form, { 'render_rest': false } ) }}
Run Code Online (Sandbox Code Playgroud)
还有控制器:
if ($form->isSubmitted() && $form->isValid()) {
$pdf = $salaire->getPdf();
if ($pdf == null) { /* Always true */
$salaire->setPdf('test');
}
/* operations to extract the file name and set it to the pdf variable in salaire */
$em->persist($salaire);
$em->flush();
return $this->redirectToRoute('salaires_index', array("id" => $remuneration->getId()));
}
Run Code Online (Sandbox Code Playgroud)
问题是即使我输入一个文件,当我得到输入的值时
->salaire->getPdf()
Run Code Online (Sandbox Code Playgroud)
结果始终为null.
首先我想是因为我在我的实体中的字符串上设置了表单输入FileType,但我试图在我的实体中的UploadedFile变量上设置它,结果仍为null.
谢谢你的帮助.
请务必设置表单元素的正确 enctype(在表单标记中设置 value enctype="multipart/form-data")。
参考文档,您可以执行以下操作:
{{ form_start(form, {'multipart': true}) }}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |