How to dynamically load dropdown options in Symfony without JavaScript?

I have a Symfony form with two dropdown fields that need to work together. Here’s my current setup:

'country' => new sfWidgetFormChoice(array('choices' => CountryPeer::fetchAllCountries())),
'region' => new sfWidgetFormChoice(array('choices' => RegionPeer::fetchAllRegions())),

What I need 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 country. The tricky part is I want to avoid using any JavaScript for this functionality. Is there a way to handle this server-side filtering in Symfony? I’m looking for a clean backend solution that can update the form options based on the user’s selection.

hmm interesting challenge! have you considered using symfony’s form events like PRE_SET_DATA or PRE_SUBMIT? you could listen for form submission and modify the region widget based on whats posted. curious tho - is there a specific reason you’re avoiding javascript? seems like it would make the ux much smoother

another option is to use multiple submit buttons or even seperate the form into steps. first step shows country dropdown with a ‘next’ button, then redirect to second step with filtered regions based on selected country. bit more work but keeps everything server-side and avoids the onchange reload approach

The most straightforward approach without JavaScript is implementing a page reload mechanism when the country selection changes. You can add an onchange event to your country dropdown that submits the form automatically, then rebuild the region dropdown based on the posted country value. In your form class, check if a country value exists in the request parameters during form initialization. If present, filter your region choices accordingly: if ($this->getObject()->getCountry() || $request->getParameter(‘country’)) { $countryId = $this->getObject()->getCountry() ?: $request->getParameter(‘country’); $this->setWidget(‘region’, new sfWidgetFormChoice(array(‘choices’ => RegionPeer::fetchRegionsByCountry($countryId)))); } While this creates a slight user experience interruption due to the page refresh, it maintains your no-JavaScript requirement and provides clean server-side logic. The form will remember the selected country and populate appropriate regions after each reload.