-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] TransformationFailedException: Support specifying message to display #20978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Form] TransformationFailedException: Support specifying message to display #20978
Conversation
src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php
Show resolved
Hide resolved
src/Symfony/Component/Form/Exception/TransformationFailedException.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Exception/TransformationFailedException.php
Outdated
Show resolved
Hide resolved
@HeahDude : Is it worth working again on this PR or should I wait your propositions about #20978 (comment) ? |
Yes, I suggest to wait :). |
Moving to 4.1. Rebase on master needed, where PHP 7.1 features can be used btw. |
@HeahDude Any ways we can unblock this PR? |
What about allowing the option to be a closure that would get passed the transformation failure exception and would return the error message to use? |
@xabbuh : Would you remove the safe message in favor of more specific |
After thinking about it again I think having a message key in the exception isn't such a bad idea. It's also consistent with what we support in the Security component. |
Woops. I've closed this one by mistake. |
rebase needed. for which branch is this PR? |
Rebased on master |
src/Symfony/Component/Form/Exception/TransformationFailedException.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Form.php
Outdated
@@ -1050,7 +1050,13 @@ private function modelToNorm($value) | |||
$value = $transformer->transform($value); | |||
} | |||
} catch (TransformationFailedException $exception) { | |||
throw new TransformationFailedException('Unable to transform value for property path "'.$this->getPropertyPath().'": '.$exception->getMessage(), $exception->getCode(), $exception); | |||
throw new TransformationFailedException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be kept on one line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, not sure if we really need this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we really need this change
Well, we need to forward the invalid message and parameters to the new exception, otherwise it's just vanished. What did you mean?
@ogizanagi @HeahDude I think we need to take a decision here. Finish/merge or close? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this is valuable and doesn't bring much complexity.
src/Symfony/Component/Form/Form.php
Outdated
@@ -1050,7 +1050,13 @@ private function modelToNorm($value) | |||
$value = $transformer->transform($value); | |||
} | |||
} catch (TransformationFailedException $exception) { | |||
throw new TransformationFailedException('Unable to transform value for property path "'.$this->getPropertyPath().'": '.$exception->getMessage(), $exception->getCode(), $exception); | |||
throw new TransformationFailedException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we really need this change
Well, we need to forward the invalid message and parameters to the new exception, otherwise it's just vanished. What did you mean?
(PR rebased) |
Thank you @ogizanagi. |
…ng message to display (ogizanagi) This PR was merged into the 4.3-dev branch. Discussion ---------- [Form] TransformationFailedException: Support specifying message to display | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22501 | License | MIT | Doc PR | N/A Currently, a failed transformation can't display a very accurate message, as it only uses the value of the `invalid_message` option. I suggest to add more flexibility, somehow the same way we did for `CustomUserMessageAuthenticationException`. A test case in `FormTypeTest` shows a use-case based on @webmozart's [blog post about value/immutable objects in Symfony forms](https://webmozart.io/blog/2015/09/09/value-objects-in-symfony-forms/). ~I named the exception properties and methods the same way as for `CustomUserMessageAuthenticationException`, but do you think the followings would be better:~ - ~`setSafeMessage` ➜ `setInvalidMessage`~ - ~`getMessageKey` ➜ `getInvalidMessageKey`~ - ~`getMessageData` ➜ `getInvalidMessageParameters`~ ~in order to echoes `invalid_message` and `invalid_message_parameters` options?~ => Replaced to use `invalidMessage` & `invalidMessageParameters` Commits ------- d11055c [Form] TransformationFailedException: Support specifying message to display
Currently, a failed transformation can't display a very accurate message, as it only uses the value of the
invalid_message
option. I suggest to add more flexibility, somehow the same way we did forCustomUserMessageAuthenticationException
.A test case in
FormTypeTest
shows a use-case based on @webmozart's blog post about value/immutable objects in Symfony forms.I named the exception properties and methods the same way as forCustomUserMessageAuthenticationException
, but do you think the followings would be better:setSafeMessage
➜setInvalidMessage
getMessageKey
➜getInvalidMessageKey
getMessageData
➜getInvalidMessageParameters
in order to echoesinvalid_message
andinvalid_message_parameters
options?=> Replaced to use
invalidMessage
&invalidMessageParameters