I’m working with Symfony forms and need help with cascading dropdowns. Here’s my current setup:
'country' => new sfWidgetFormChoice(array('choices' => CountryPeer::fetchAllCountries())),
'region' => new sfWidgetFormChoice(array('choices' => RegionPeer::fetchAllRegions()))
What I want to achieve is when a user picks a country from the first dropdown (for example USA), the second dropdown should automatically show only the regions that belong to that selected country. The tricky part is I need this to work on the server side without any client-side scripting or Ajax calls. Is there a way to handle this kind of dependent dropdown behavior purely through Symfony’s backend form processing? I’ve been searching for solutions but most examples involve JavaScript which I want to avoid for this particular implementation.
You’ll need to set up form resubmission logic so when someone picks a country, the form reloads with filtered regions. Modify your form config method to take a country parameter and populate region choices based on that. When the country dropdown changes, resubmit the form to the server. The server rebuilds the form using RegionPeer::fetchRegionsByCountry($countryId) instead of grabbing all regions. The downside? You get a page refresh every time someone selects a country, which isn’t as smooth as client-side solutions. But it keeps everything server-side controlled and works great when you can’t use JavaScript.
Interesting challenge! Why avoid JavaScript tho? Legacy system or accessibility constraints? Maybe form events could work when the form resubmits?
you can use form events like preSubmit or preSetData to rebuild region choices when someone picks a country. the form regenerates with filtered regions after submission. won’t be as smooth as js, but it works if you’re sticking to server-side only.