简体   繁体   中英

Dynamically modify forms in Symfony2 without Javascript?

I was reading the How to dynamically Generate Forms Based on user Data from the Symfony2 cook book.

I'm seeking functionality similar to the link above.

However, if I understand correctly it suggests to use AJAX to update the form.

Is this correct?

This example is desired functionality I'm seeking:

Step 1:

Form //Controller locationAction
 ________________
|
|
| Country Select 
|
| |Please select...|
|
|      ____
|     |Next|
 _______________

Expectations after Hitting Next

  • Submits to the same locationAction method
  • form appears with previously selected result.
  • Skips validation until all dynamic fields are present.
  • New State choice field appears, choices depend on user submitted Country field.

Step 2:

|
v

Form //Controller locationAction
 ________________
|
|
| Country Select 
|
|      |U.S.|
|
|  
|      State
|  |Please select ....| //List is displayed that corresponds to their selected country
|
|      ______
|     |Submit|
 _______________

After pushing submit

  • Validates both Country and State then performs an action that depends on the form (dynamic fields included) being validated first.

Is it possible to obtain the above result using Form Subscribers/Form Listeners alone in one form with no JavaScript?

Or do I need to use JavaScript/create a separate form?

However, if I understand correctly it suggests to use AJAX to update the form.

No it doesn't. You can just submit the form to the same action. After each step, it's a new request/response and the dynamic form generation is handled at server side, not by javascript.

And perhaps, dynamic-generation-for-submitted-forms is exactly your use case (it's another section on the same guide).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM