Symfony2数据变压器,获取异常消息 [英] Symfony2 data transformers, getting exception message

查看:93
本文介绍了Symfony2数据变压器,获取异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的数据变换器,如专用菜谱,这是我的逆转换:

I've created my own data transformer, as explained in the dedicated cookbook, here is my reverse transformation:

public function reverseTransform($val)
{
    // ...
    // My logic here
    // ...

    // If $val is not valid
    throw new TransformationFailedException(
        'My custom error message'
    );
}

问题是:如何获得自定义错误消息?我想将其显示为我的表单字段的错误消息。如何做?

The question is: how do I get the "custom error message" thrown? I would like to display it as the error message of my form field. How do I do that?

谢谢!

推荐答案

是:你没有变压器的工作是要好好转换,而不是进行错误检查。

Sort answer is: You don't. The transformers job is to, well, transform and not to do error checking.

将一个约束添加到字段中,该字段将检查转换的值并处理错误消息。

Add a constraint to the field which will check the transformed value and take care of error messaging.

这篇关于Symfony2数据变压器,获取异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆