以symfony形式上传文件
我在从表单上载文件时遇到问题。 我做错了什么?
错误为:
需要类型为";AppEntityImage";、实例为";的参数 在酒店提供的SymfonyComponentHttpFoundationFileUploadedFile"; 路径&q;文件&q;。 表单->;handleRequest(Object(Request))中 C:xampphtdocsiurosrcControllerOfferController.php(第29行)
我的代码:
类OfferType:
class OfferType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('description', TextareaType::class)
->add('files',FileType::class, [
'multiple' => true
]
)
类OfferController:
class OfferController extends AbstractController
{
public function createOffer(Request $request) {
$offer = new Offer();
$form = $this->createForm(OfferType::class, $offer);
$form->handleRequest($request);
类实体报价:
class Offer
{
/**
* @ORMOneToMany(targetEntity="Image", mappedBy="offer", cascade={"persist"})
*/
private $files;
模板:
<table>
<tbody>
<tr>
<th>Nazwa:</th>
<td>{{ form_widget(form.name) }}</td>
</tr>
<tr>
<th>Opis:</th>
<td>{{ form_widget(form.description) }}</td>
</tr>
<tr>
<th>Zdjęcia:</th>
<td>{{ form_widget(form.files) }}</td>
</tr>
<tr>
<th></th>
<td>{{ form_widget(form.save) }}</td>
</tr>
</tbody>
</table>
解决方案
我推荐捆绑包VichUploader
composer require vich/uploader-bundle
# config/services.yaml
parameters:
app.path.product_images: /uploads/images/products
# ...
# config/packages/vich_uploader.yaml
vich_uploader:
# ...
mappings:
product_images:
uri_prefix: '%app.path.product_images%'
upload_destination: '%kernel.project_dir%/public%app.path.product_images%'
``
相关文章