【问题标题】:Symfony file field empty on editSymfony 文件字段在编辑时为空
【发布时间】:2017-09-13 23:37:46
【问题描述】:

我正在阅读一段时间,但没有发现任何适合我的东西。 这是实体

/**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Molimo unesite PDF ili Word fajl.")
 * @Assert\File(mimeTypes={ "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-powerpoint", "application/pdf", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"})
 */
private $body;

这是表格

// Problem with empty field on edit persist! Form expect FileType but db sends string!!!
            ->add('body', FileType::class,  [
                //'data_class' => null,// <-- If I change it nothing happened.
                'label' => 'Word ili pdf dokument',
                'invalid_message' =>'Id posta nije validan',
                'attr' => [
                    'class' => 'form-group',
        ]])

一切都非常简单,并按照文档制作。我的实体包含很少的属性,其中一个是文件($body)。文件设置为保存在 web/uploads/post/ 中。当我转到编辑页面时,我收到此错误:

"表单的视图数据应该是 Symfony\Component\HttpFoundation\File\File 类的实例,但它是一个 (n) 字符串。您可以通过将“data_class”选项设置为 null 来避免这种情况或者通过添加一个将字符串转换为 Symfony\Component\HttpFoundation\File\File 实例的视图转换器。

如果我设置 data_status => null 字段为空。

我需要一些链接到这个工作的例子,Data Transformer 可能吗?

【问题讨论】:

  • 其实欢迎提出任何建议!
  • 能否就您所面临的问题向我们提供更多信息?
  • 我做了一些修改。如果需要任何信息,我准备好写了。

标签: php forms symfony


【解决方案1】:

我在编辑操作中遇到了同样的问题,并通过手动提交表单并将 clearMissing 参数设置为 false 而不是使用 $form-&gt;handleRequest($request); 来解决它,如 https://symfony.com/doc/current/form/direct_submit.html 中所述:

if ($request->isMethod('POST'))
{
    $form->submit($request->request->get($form->getName()),false);

    if ($form->isSubmitted() && $form->isValid())
    {

这样,文件的原始值不会被设置为空,以防你不提交新文件。

【讨论】:

    【解决方案2】:

    您可以通过在您的 editAction 中创建一个新的 File() 来解决这个问题。像这样:

    /**
     * @Route("/edit/{yourEntity}", name="entity_edit")
     * 
     * @return Response
     */
    public function editAction(Request $request, YourEntity yourEntity)
    {
        $body = new File($this->getParameter('your_files_directory') . '/' . $yourEntity->getBody());
        $yourEntity->setBody($body);
        // rest of your editAction
    }
    

    【讨论】:

    • 谢谢!在我实现了你的代码之后。我仍然让文件字段为空,但是在我从表单中删除该字段后,编辑没有例外。不好,我知道,但我确实需要更改文件名。再次感谢!
    【解决方案3】:

    好的,解决了。 @ASOlivieri 的答案很好,只需稍作修改。他的答案中的代码将记住带有文件名的路径。因此,如果您想再次对其进行编辑,它将抛出未找到的异常。您必须再次 ->setBody 与原始文件名相同。在flush()之前设置

    public function editAction(Request $request, Post $post) {
        $fileName = $post->getBody();
        $file = new File($this->getParameter('post_directory') . '/' . $post->getBody());
        $post->setBody($file);
    
        $deleteForm = $this->createDeleteForm($post);
        $editForm = $this->createForm('AppBundle\Form\PostEditType', $post);
        $editForm->handleRequest($request);
    
    
        if ($editForm->isSubmitted() && $editForm->isValid()) {
    
            $post->setBody($fileName);
            $this->getDoctrine()->getManager()->flush();
    

    希望这会有所帮助!

    【讨论】:

    • 很高兴你知道了!
    【解决方案4】:

    在我的情况下,有帮助的是:

    if ($request->isMethod('POST')) {
            $form->submit($request->request->get($form->getName()));
            if ($form->isValid()) {...
    

    $form->handleRequest($request);
    if ($form->isSubmitted()) {
         if ($form->isValid()) { ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多