Skip to content

[Form] How to use expressions in forms without a data class #19720

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

Open
pauljura opened this issue Mar 28, 2024 · 3 comments
Open

[Form] How to use expressions in forms without a data class #19720

pauljura opened this issue Mar 28, 2024 · 3 comments
Labels
Form hasPR A Pull Request has already been submitted for this issue.
Milestone

Comments

@pauljura
Copy link

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.

image

The code might look like this:

$builder
    ->add('how_did_you_hear', ChoiceType::class, [
        'required' => true,
        'label' => 'How did you hear about us?',
        'choices' => [
            'Search engine' => 'search_engine',
            'Friends' => 'friends',
            'Other' => 'other',
        ],
        'expanded' => true,
        'constraints' => [
            new Assert\NotBlank(),
        ]
    ])
    ->add('other_text', TextType::class, [
        'required' => false,
        'label' => 'Please specify',
        'constraints' => [
            new Assert\When(
                expression: 'this.getParent().get("how_did_you_hear").getData() == "other"',
                constraints: [
                    new Assert\NotBlank(),
                ],
            )
        ],
    ])
;

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

@carsonbot carsonbot added the Form label Mar 28, 2024
@javiereguiluz
Copy link
Member

@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!

@xabbuh
Copy link
Member

xabbuh commented Aug 1, 2024

The solution @pauljura describes looks correct to me if you want to use a form without an underlying class. Though I am not sure where the explanation should be put. https://symfony.com/doc/current/form/without_class.html is probably the best place.

@fg-ok
Copy link

fg-ok commented Aug 19, 2024

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.

Maybe as sub below "Constraints At Class Level" titled "Field dependent constraints" on https://symfony.com/doc/current/form/without_class.html

@javiereguiluz javiereguiluz added this to the 6.4 milestone Jun 4, 2025
@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Form hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

5 participants