2

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
  • I have managed to do what you need. Try recreating exactly what is on the cookbook and it should work. Then compare it with our code. Commented Nov 1, 2012 at 8:57

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So why does the TransformationFailedException exception takes a string as an argument in the cookbook?
Good question and I wondered that myself when I first ran into this some time ago. I assume it's a separation of concerns thing. The idea is to ensure that your entity contains the correct data regardless of how it get's there. Keep in mind that the transformers get called by the form->bind() and not by form->isValid();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.