【问题标题】:Constraints violations messages are not displayed不显示约束违规消息
【发布时间】:2019-09-26 14:28:18
【问题描述】:

我正在尝试一种上传多个文件的方法。

我在 buildForm 方法中添加了一个约束以允许某些类型的文件。但是,当文件类型不好时,{{ form_errors(form.documents) }}不会显示错误信息。

这是我的表格:

票务类型:

->add('documents', CollectionType::class, array(
            'entry_type'        => TicketDocumentType::class,
            'prototype'         => true,
            'allow_add'         => true,
            'allow_delete'      => true,
            'by_reference'      => false,
            'required'          => false,
            'label'             => false,
        ))

TicketDocumentType :

->add('file', FileType::class, array(
                'label'     => false,
                'required'  => true,
                'constraints' => [
                    new File([
                        'maxSize' => '400k',
                        'mimeTypes' => [
                            "image/png",
                            "image/jpeg",
                            "image/jpg",
                            "image/gif",
                            "image/x-citrix-jpeg",
                            "image/x-citrix-png",
                            "image/x-png",
                            "application/pdf",
                            "application/x-pdf",
                            "application/vnd.ms-excel",
                            "application/msword",
                            "text/plain",
                            "application/zip"
                        ],
                        'mimeTypesMessage' => 'Les formats autorisés sont PDF, TXT, DOC, XLS, JPG, PNG, GIF, ZIP',
                    ])
                ]
            ));

经过一番研究,我发现我可以将'error_bubbling' => true{{ form_errors(form) }} 一起使用。

但是,我希望只有 form.documents 错误。这可能吗?

【问题讨论】:

    标签: symfony constraints symfony4


    【解决方案1】:

    应该只是直接针对文档的情况:

    {{ form_errors(form.documents) }}

    查看这里了解更多信息:https://symfony.com/doc/current/form/form_customization.html#form-rendering-functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2011-04-19
      • 2022-01-23
      • 2019-09-22
      相关资源
      最近更新 更多