最好的方法是在symfony 3中自定义field_error

时间:2017-09-06 09:50:46

标签: symfony twig

我有一个带路径字段的实体

  /**
     * @var string
     * @Assert\NotBlank(message="Please, upload scontrino/fattura della stufa acquistata.")
     * @Assert\File(mimeTypes={ "application/pdf","image/png" ,"image/jpg","image/jpeg" })
     * @ORM\Column(name="path_file", type="string", length=255)
     */
    private $path;

我有一个表格

的字段
->add('path', FileType::class, array('data_class' => null))
;

并在twig文件中

<div class="row">
                                                    <div class="input-field col s12 ">
                                                         {{ form_errors(form.path) }}
                                                        {{ form_widget(form.path) }}

                                                    </div>
                                                </div>

当我按下提交按钮并且我现在不允许文件我的消息是

Il mime type del file non è valido ("application/zip"). I tipi permessi sono "application/pdf", "image/png", "image/jpg", "image/jpeg".

我想改变那条消息,但我不明白最好的方法。 如果我必须在实体文件或表单文件或twig文件中执行此操作

1 个答案:

答案 0 :(得分:2)

您必须添加:     mimeTypesMessage = "with the text you want",位于您实体的@Assert\File in your @Assert\File

/**
 * @var string
 * @Assert\NotBlank(message="Please, upload scontrino/fattura della stufa acquistata.")
 * @Assert\File(mimeTypes={ "application/pdf","image/png" ,"image/jpg","image/jpeg" }, mimeTypesMessage = "with the text you want")
 * @ORM\Column(name="path_file", type="string", length=255)
 */

The symfony Reference