Symfony2 data transformers, getting exception mess

2020-04-15 05:55发布

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?

Thanks!

1条回答
▲ chillily
2楼-- · 2020-04-15 06:11

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.

查看更多
登录 后发表回答