Drupal website specialists.

Making forms with Symfony :: partial layout example

Posted 19/July/2008 by neubreed

Example using symfony form helpers:

<fieldset>
<legend>Personal Information</legend>
<label for="title"><span>Title<em>(required)</em></span><?php echo select_tag('title', options_for_select(sfConfig::get('app_title'), $member->getTitle(), array('include_custom' => 'Select')),highlight_error('title'));?></label>
<label for="first_name"><span>First name<em>(required)</em></span><?php echo object_input_tag($member, 'getFirstName', highlight_error('first_name'));?></label>
<label for="last_name"><span>Last name<em>(required)</em></span><?php echo object_input_tag($member, 'getLastName', highlight_error('last_name'));?></label>
<div class="clear"></div>
<?php $dob = $member->getDateOfBirth() === null ? '' : $member->getDateOfBirth(); ?>
<label for="date_of_birth"><span>Date of birth<em>*</em></span><?php echo input_date_tag('date_of_birth', $dob, highlight_error('date_of_birth', 
                array ('order'=> array('d','m','y'),
                 'include_custom' => array ('day'=>'Day','month'=>'Month','year'=>'Year'),
                 'year_start'=>'1928',
                 'year_end'=>'1992')));?> </label>
</fieldset>

The above code will output the following:

<fieldset>
<legend>Personal Information</legend>
<label for="title"><span>Title<em>*</em></span>
<select name="title" id="title">
  <option value="">Select</option>
  <option value="Mr">Mr</option>
  <option value="Ms">Ms</option>
  <option value="Mrs">Mrs</option>
  <option value="Dr">Dr</option>
  <option value="Rt">Rt Hon.</option>
  <option value="Rev">Rev</option>
  <option value="Mdme">Mdme</option>
  <option value="Sir">Sir</option>
</select>
</label>
<label for="first_name"><span>First name<em>*</em></span>
<input type="text" name="first_name" id="first_name" value="" />
</label>
<label for="last_name"><span>Last name<em>*</em></span>
<input type="text" name="last_name" id="last_name" value="" />
</label>
<div class="clear"></div>
<label for="date_of_birth"><span>Date of birth<em>*</em></span>
<select name="date_of_birth[day]" id="date_of_birth_day">
  <option value="" selected="selected">Day</option>
  <option value="1">01</option>
  ... and so on ...
  <option value="30">30</option>
  <option value="31">31</option>
</select>
-
<select name="date_of_birth[month]" id="date_of_birth_month">
  <option value="" selected="selected">Month</option>
  <option value="1">January</option>
   ... and so on ...
  <option value="12">December</option>
</select>
-
<select name="date_of_birth[year]" id="date_of_birth_year">
  <option value="" selected="selected">Year</option>
  <option value="1928">1928</option>
   ... and so on ...
  <option value="1992">1992</option>
</select>
</label>
</fieldset>