You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think it would be worth adding a section on how to use expressions in forms without a data class, because I recently wanted to do this and couldn't find any documentation on it.
A simple example would be a "how did you hear about us?" form, with a selection of radio buttons, and a text input. If the "other" option is chosen, then we want the text input to be non-blank so the user is required to fill something in, otherwise it can remain blank.
I guessed at the expression through trial and error, but it works and I think it may come in handy for others.
From my understanding, "this" refers to the current form element, so to get the value of the other form element I have to first go up to the parent, then fetch the element in question, and get its value with getData. Please correct me if this is not right, or not the best way to go about it.
I think this would be handy to include on this page: https://symfony.com/doc/current/form/without_class.html but personally I would not look here first, because I already know how to use forms without a class, I just needed more information on which validator to use and how to configure it.
@xabbuh just asking: is this an expected behavior when using expressions in form constraints? I thought the "right way" to solve things like this was using form events. Thanks!
Thanks to @pauljura! I had the problem that although I have an underlying data class it's the data class of a form collection and neither constraints via Attributes nor configureOptions() worked. The above solution did it, so I appreciate it if you'll add the explanation.
I think it would be worth adding a section on how to use expressions in forms without a data class, because I recently wanted to do this and couldn't find any documentation on it.
A simple example would be a "how did you hear about us?" form, with a selection of radio buttons, and a text input. If the "other" option is chosen, then we want the text input to be non-blank so the user is required to fill something in, otherwise it can remain blank.
The code might look like this:
I guessed at the expression through trial and error, but it works and I think it may come in handy for others.
From my understanding, "this" refers to the current form element, so to get the value of the other form element I have to first go up to the parent, then fetch the element in question, and get its value with getData. Please correct me if this is not right, or not the best way to go about it.
I think this would be handy to include on this page: https://symfony.com/doc/current/form/without_class.html but personally I would not look here first, because I already know how to use forms without a class, I just needed more information on which validator to use and how to configure it.
So I looked here here: https://symfony.com/doc/current/reference/constraints/Expression.html and here: https://symfony.com/doc/current/reference/constraints/When.html but these pages currently assume the validators are being used on an entity, and have no documentation on how to make them work on a form without a data class. So adding some help to these pages would also be useful.
Thanks
The text was updated successfully, but these errors were encountered: