douyingp82418 2017-05-02 14:26
浏览 21

Symfony 2.8避免回调的双重错误

In my entity I have the following attribute:

/**
 * @var string
 *
 * @ORM\Column(name="programm", type="string", length=255, nullable=true)
 * @Assert\File(
 *     maxSize = "1M",
 *     mimeTypes = {"application/pdf", "application/x-pdf", "image/png", "image/jpg", "image/jpeg"},
 *     mimeTypesMessage = "Es sind nur pdf, jpg und png Dateien erlaubt",
 *     groups={"new"}
 * )
 */
private $programm;

and a Callback

 /**
 * @Assert\Callback
 */
public function validateFields(ExecutionContextInterface $context) {
    if ('' == $this->programm && 2 == $this->programmextern) {
        $context->buildViolation('Bitte Datei hochladen')->atPath('programm')->addViolation();
    }

If the Assert\File gives an error (file too big, file doesn't have the proper mimeType, etc) I want to skip the Callback. Is there a way to access the errors produced by Assert\File in my entity, so I can trigger my Callback only if there aren't any errors?

If not, is there a way to filter errors so that, if there is more than one error, only the AssertFile error is displayed in the twig template?

  • 写回答

1条回答 默认 最新

  • dongrang9300 2017-05-02 14:48
    关注

    Then most simple option comes in mind to achieve what you want is to add a different validation group to the callback:

    /**
     * @Assert\Callback(
     *     groups={"trigger_after_new"}
     * )
     */
    public function validateFields(ExecutionContextInterface $context) {
        if ('' == $this->programm && 2 == $this->programmextern) {
            $context->buildViolation('Bitte Datei hochladen')->atPath('programm')->addViolation();
    }
    

    and then read How to Sequentially Apply Validation Groups to trigger the callback only if previous validator groups passed with success.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构