dongyan2469 2019-05-14 22:52
浏览 35

Symfony表单数据未使用自定义表单图像类型主题发送

I am using a plugin called FilePond to have a better look while uploading an image with VichUploaderBundle. So to evoid the default form_row template of the bundle i made a custom one:

//template/forms/upload_image.html.twig
{% block _image_file_row %}
    <input type="file" class="filepond-initialise" name="image[file][file]" required="required" accept=".gif, .jpg, .png">
{% endblock %}

Then i render the template in the page:

{% form_theme form 'forms/upload_image.html.twig' %}
{{ form_start(form) }}
    {{ form_row(form.file) }}
    <h3>Tags</h3>
        <ul class="tags list-inline" data-prototype="{{ form_widget(form.tags.vars.prototype.name)|e('html_attr') }}">
        {% for tag in form.tags %}
            <li>{{ form_row(tag.name) }}</li>
        {% endfor %}
    </ul>
    <button class="btn btn-success btn-block" type="submit">Upload</button>
    <input type="hidden" name="token" value="{{ csrf_token('image-upload') }}">
{{ form_end(form, {'render_rest': false}) }}

And this is the action in the controller:

public function upload(Request $request):Response
    {
        $image = new Image();
        $form = $this->createForm(ImageType::class, $image);
        $token = $request->request->get('token');
        $form->handleRequest($request);

        if ($form->isSubmitted() && $this->isCsrfTokenValid('image-upload', $token)) {
            dump($image);
            dump($form->getData());
            $this->em->persist($image);
            $this->em->flush();
            return $this->redirectToRoute('admin_image_index');
        }

        return $this->render('admin/image/upload.html.twig', array(
            'form' => $form->createView()
        ));
    }

The problem that the content of the file input doesn't seem to be sending in the form submit. i get an error of name property null:

An exception occurred while executing 'INSERT INTO image (name, updated_at) VALUES (?, ?)' with params [null, "2019-05-14 22:42:18"]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

and the dumping of the $image variable and $form-getData() as below:

Image {#496 ▼
  -id: null
  -file: null
  -name: null
  -updatedAt: DateTime @1557873995 {#497 ▼
    date: 2019-05-14 22:46:35.496026 UTC (+00:00)
  }
  -tags: ArrayCollection {#498 ▼
    -elements: array:3 [▼
      0 => Tag {#901 ▼
        -id: null
        -name: "sport"
        -images: ArrayCollection {#902 ▶}
      }
      1 => Tag {#878 ▼
        -id: null
        -name: "nature"
        -images: ArrayCollection {#905 ▶}
      }
      2 => Tag {#906 ▼
        -id: null
        -name: "fashion"
        -images: ArrayCollection {#908 ▶}
      }
    ]
  }
}

While the tags inputs are working fine, means that the problem is with file input.

Here is the builder in ImageType.php the rest is by default :

$builder
            ->add('file', VichImageType::class)
            ->add('tags', CollectionType::class, [
                'entry_type' => TagType::class,
                'entry_options' => ['label' => false],
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
            ])
        ;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口