Bernd Strehl
Bernd Strehl

Reputation: 2920

Symfony2 How to add required form field dynamically

In Form-Builder, I have a choice of fields for a Credit Card or Bill Payment Type.

Depending on what is selected, other form fields will be loaded i.e. CC-Number or Bank Account.

I need the CC-Number input to be a required field, if Credit Card is selected. How can I achieve this?

Upvotes: 0

Views: 2627

Answers (2)

Bernd Strehl
Bernd Strehl

Reputation: 2920

I found a solution that fits my needs by myself:

If a field is required it has the attribute required="required". So i did a little piece of jQuery, which removes or adds this attribute on elements that are hidden or shown.

Upvotes: 1

Damien
Damien

Reputation: 5882

You can follow this cookbook entry for the backend side, the Javascript code needed on the client side is up to you.

Upvotes: 1

Related Questions