Symfony: Drop Down List Box Without Submit Button

   Posted November 25, 2009 by David Kirk in Symfony

I needed a drop down list box for a symfony site I was designing. As a newbie symfony programmer, I wanted to record this so I could reference it later. Although this uses a little javascript, it will correctly function and is backwards compatible to users without javascript enabled.

I searched for a while for the proper way to do this in symfony. Hopefully this will save somebody else a little time.

First, here is the code:

<?php echo form_tag("module/action"); ?>
<?php echo select_tag('selection', options_for_select($list,null,array('include_custom' => '-- Jump to List --')), array('onChange' => 'Javascript:this.form.submit();')) ?>
<noscript><?php echo submit_tag('go'); ?></noscript>

I’ll walk through the not obvious parts.

First is the normal form_tag with your return module and action.

Next, selection is your returned selection. $list is your list of objects.

array(‘include_custom’ => ‘– Jump to List –‘) creates a unique initial field or title for the list.

array(‘onChange’ => ‘Javascript:this.form.submit();’ is the javascript that executes the form on field change.

The submit_tag in the noscript tags allows the submit button to only be seen by those without javascript enabled. This keeps your design backward compatible to those without javascript.

If you are lost, see the symfony forms reference for additional details.


About David Kirk

David Kirk is one of the original founders of tech-recipes and is currently serving as editor-in-chief. Not only has he been crafting tutorials for over ten years, but in his other life he also enjoys taking care of critically ill patients as an ICU physician.
View more articles by David Kirk

The Conversation

Follow the reactions below and share your own thoughts.

Leave a Reply