symfony API

FormHelper Functions

« Back to API menu

action

addon

cache

config

controller

countable

database

debug

exception

filter

generator

helper

i18n

log

request

response

storage

test

user

util

validator

view

You are currently browsing the symfony documentation for the 1.0 version. Switch to:

FormHelper.

Function Summary

  • string reset_tag($value = 'Reset', $options = array())
    Returns an XHTML compliant <input> tag with type="reset".
  • array _convert_options($options)
    Converts specific to their correct HTML format
  • string options_for_select($options = array(), $selected = '', $html_options = array())
    Returns a formatted set of <option> tags based on optional array variable.
  • string input_tag($name, $value = null, $options = array())
    Returns an XHTML compliant <input> tag with type="text".
  • string submit_image_tag($source, $options = array())
    Returns an XHTML compliant <input> tag with type="image".
  • string form_tag($url_for_options = '', $options = array())
    Returns an HTML <form> tag that points to a valid action, route or URL as defined by .
  • string input_file_tag($name, $options = array())
    Returns an XHTML compliant <input> tag with type="file".
  • string input_hidden_tag($name, $value = null, $options = array())
    Returns an XHTML compliant <input> tag with type="hidden".
  • string submit_tag($value = 'Save changes', $options = array())
    Returns an XHTML compliant <input> tag with type="submit".
  • string input_date_tag($name, $value = null, $options = array())
    Returns an XHTML compliant <input> tag to be used as a free-text date field.
  • string textarea_tag($name, $content = null, $options = array())
    Returns a <textarea> tag, optionally wrapped with an inline rich-text JavaScript editor.
  • _convert_include_custom_for_select($options, [[[amp]]]$select_options)
  • string select_language_tag($name, $selected = null, $options = array())
    Returns a <select> tag populated with all the languages in the world (or almost).
  • string select_tag($name, $option_tags = null, $options = array())
    Returns a <select> tag, optionally comprised of <option> tags.
  • string radiobutton_tag($name, $value, $checked = false, $options = array())
    Returns an XHTML compliant <input> tag with type="radio".
  • string label_for($id, $label, $options = array())
    Returns a <label> tag with for the specified parameter.
  • string select_country_tag($name, $selected = null, $options = array())
    Returns a <select> tag populated with all the countries in the world.
  • string checkbox_tag($name, $value = '1', $checked = false, $options = array())
    Returns an XHTML compliant <input> tag with type="checkbox".
  • string get_id_from_name($name, $value = null)
    Returns a formatted ID based on the parameter and optionally the parameter.
  • string input_password_tag($name = 'password', $value = null, $options = array())
    Returns an XHTML compliant <input> tag with type="password".
  • string input_date_range_tag($name, $value, $options = array())
    Returns two XHTML compliant <input> tags to be used as a free-text date fields for a date range.

Function Details

  • (string) reset_tag ($value = 'Reset', $options = array())

    $value field value (title of reset button)
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;reset&quot;.

    By default, this helper creates a submit tag with a name of <em>reset</em>. Also, the default $value parameter (title of the button) is set to "Reset" which can be easily overwritten by passing a $value parameter.
    Examples:

      echo reset_tag();

      echo reset_tag('Start Over');

    returns XHTML compliant <input> tag with type="reset"

  • (array) _convert_options ($options)

    $options options

    Converts specific to their correct HTML format

    returns returns properly formatted options

  • (string) options_for_select ($options = array(), $selected = &apos;&apos;, $html_options = array())

    $options dataset to create <option> tags and <optgroup> tags from
    $selected selected option value
    $html_options additional HTML compliant <option> tag parameters

    Returns a formatted set of &lt;option&gt; tags based on optional array variable.

    The options_for_select helper is usually called in conjunction with the select_tag helper, as it is relatively useless on its own. By passing an array of $options, the helper will automatically generate <option> tags using the array key as the value and the array value as the display title. Additionally the options_for_select tag is smart enough to detect nested arrays as <optgroup> tags. If the helper detects that the array value is an array itself, it creates an <optgroup> tag with the name of the group being the key and the contents of the <optgroup> being the array.
    Options:


    • include_blank - Includes a blank <option> tag at the beginning of the string with an empty value

    • include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value



    Examples:
      echo select_tag('person', options_for_select(array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly')));

      $card_list = array('VISA' => 'Visa', 'MAST' => 'MasterCard', 'AMEX' => 'American Express', 'DISC' => 'Discover');
    
    echo select_tag('cc_type', options_for_select($card_list, 'AMEX', array('include_custom' => '-- Select Credit Card Type --')));

      $optgroup_array = array(1 => 'Joe', 2 => 'Sue', 'Group A' => array(3 => 'Mary', 4 => 'Tom'), 'Group B' => array(5 => 'Bill', 6 =>'Andy'));
    
    echo select_tag('employee', options_for_select($optgroup_array, null, array('include_blank' => true)), array('class' => 'mystyle'));

    returns populated with <option> tags derived from the $options array variable

  • (string) input_tag ($name, $value = null, $options = array())

    $name field name
    $value selected field value
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;text&quot;.

    The input_tag helper generates your basic XHTML <input> tag and can utilize any standard <input> tag parameters passed in the optional $options parameter.
    Examples:

      echo input_tag('name');

      echo input_tag('amount', $sf_params->get('amount'), array('size' => 8, 'maxlength' => 8));

    returns XHTML compliant <input> tag with type="text"

  • (string) submit_image_tag ($source, $options = array())

    $source path to image file
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;image&quot;.

    The submit_image_tag is very similar to the submit_tag, the only difference being that it uses an image for the submit button instead of the browser-generated default button. The image is defined by the $source parameter and must be a valid image, either local or remote (URL). By default, this helper creates a submit tag with a name of <em>commit</em> to avoid conflicts with other parts of the framework. It is recommended that you do not use the name "submit" for submit tags unless absolutely necessary.
    Examples:

      // Assuming your image is in the /web/images/ directory
    
    echo submit_image_tag('my_submit_button.gif');

      echo submit_image_tag('http://mydomain.com/my_submit_button.gif');

    returns XHTML compliant <input> tag with type="image"

  • (string) form_tag ($url_for_options = &apos;&apos;, $options = array())

    $url_for_options valid action, route or URL
    $options optional HTML parameters for the <form> tag

    Returns an HTML &lt;form&gt; tag that points to a valid action, route or URL as defined by .

    By default, the form tag is generated in POST format, but can easily be configured along with any additional HTML parameters via the optional $options parameter. If you are using file uploads, be sure to set the multipart option to true.
    Options:


    • multipart - When set to true, enctype is set to "multipart/form-data".



    Examples:
    <?php echo form_tag('@myroute'); ?>
    <?php echo form_tag('/module/action', array('name' => 'myformname', 'multipart' => true)); ?>

    returns opening HTML <form> tag with options

  • (string) input_file_tag ($name, $options = array())

    $name field name
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;file&quot;.

    Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize any standard <input> tag parameters passed in the optional $options parameter. The only difference is that it creates the tag with type="file", meaning that next to the field will be a "browse" (or similar) button. This gives the user the ability to choose a file from there computer to upload to the web server. Remember, if you plan to upload files to your website, be sure to set the multipart option form_tag helper function to true or your files will not be properly uploaded to the web server.
    Examples:

      echo input_file_tag('filename', array('size' => 30));

    returns XHTML compliant <input> tag with type="file"

  • (string) input_hidden_tag ($name, $value = null, $options = array())

    $name field name
    $value populated field value
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;hidden&quot;.

    Similar to the input_tag helper, the input_hidden_tag helper generates an XHTML <input> tag and can utilize any standard <input> tag parameters passed in the optional $options parameter. The only difference is that it creates the tag with type="hidden", meaning that is not visible on the page.
    Examples:

      echo input_hidden_tag('id', $id);

    returns XHTML compliant <input> tag with type="hidden"

  • (string) submit_tag ($value = &apos;Save changes&apos;, $options = array())

    $value field value (title of submit button)
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;submit&quot;.

    By default, this helper creates a submit tag with a name of <em>commit</em> to avoid conflicts with other parts of the framework. It is recommended that you do not use the name "submit" for submit tags unless absolutely necessary. Also, the default $value parameter (title of the button) is set to "Save changes", which can be easily overwritten by passing a $value parameter.
    Examples:

      echo submit_tag();

      echo submit_tag('Update Record');

    returns XHTML compliant <input> tag with type="submit"

  • (string) input_date_tag ($name, $value = null, $options = array())

    $name field name
    $value date
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag to be used as a free-text date field.

    You can easily implement a JavaScript calendar by enabling the 'rich' option in the $options parameter. This includes a button next to the field that when clicked, will open an inline JavaScript calendar. When a date is selected, it will automatically populate the <input> tag with the proper date, formatted to the user's culture setting. Symfony also conveniently offers the input_date_range_tag, that allows you to specify a to and from date.
    Options:


    • rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date



    Examples:
      echo input_date_tag('date', null, array('rich' => true));

    returns XHTML compliant <input> tag with optional JS calendar integration

  • (string) textarea_tag ($name, $content = null, $options = array())

    $name field name
    $content populated field value
    $options additional HTML compliant <textarea> tag parameters

    Returns a &lt;textarea&gt; tag, optionally wrapped with an inline rich-text JavaScript editor.

    The texarea_tag helper generates a standard HTML <textarea> tag and can be manipulated with any number of standard HTML parameters via the $options array variable. However, the textarea tag also has the unique capability of being transformed into a WYSIWYG rich-text editor such as TinyMCE (http://tinymce.moxiecode.com) very easily with the use of some specific options:
    Options:


    • rich: A rich text editor class (for example sfRichTextEditorTinyMCE for TinyMCE).



    Examples:
      echo textarea_tag('notes');

      echo textarea_tag('description', 'This is a description', array('rows' => 10, 'cols' => 50));

    returns <textarea> tag optionally wrapped with a rich-text WYSIWYG editor

  • _convert_include_custom_for_select ($options, [[[amp]]]$select_options)

  • (string) select_language_tag ($name, $selected = null, $options = array())

    $name field name
    $selected selected field value (two or threecharacter language/culture code)
    $options additional HTML compliant <select> tag parameters

    Returns a &lt;select&gt; tag populated with all the languages in the world (or almost).

    The select_language_tag builds off the traditional select_tag function, and is conveniently populated with all the languages in the world (sorted alphabetically). Each option in the list has a two or three character language/culture code for its value and the language's name as its display title. The country data is retrieved via the sfCultureInfo class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world. Here's an example of an <option> tag generated by the select_country_tag:
    <option value="en">English</option>
    Examples:

      echo select_language_tag('language', 'de');

    returns <select> tag populated with all the languages in the world.

  • (string) select_tag ($name, $option_tags = null, $options = array())

    $name field name
    $option_tags contains a string of valid <option></option> tags, or an array of options that will be passed to options_for_select
    $options additional HTML compliant <select> tag parameters

    Returns a &lt;select&gt; tag, optionally comprised of &lt;option&gt; tags.

    The select tag does not generate <option> tags by default. To do so, you must populate the $option_tags parameter with a string of valid HTML compliant <option> tags. Fortunately, Symfony provides a handy helper function to convert an array of data into option tags (see options_for_select). If you need to create a "multiple" select tag (ability to select multiple options), set the multiple option to true. Doing so will automatically convert the name field to an array type variable (i.e. name="name" becomes name="name[]").
    Options:


    • multiple - If set to true, the select tag will allow multiple options to be selected at once.



    Examples:
      $person_list = array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly');
    
    echo select_tag('person', options_for_select($person_list, $sf_params->get('person')), array('class' => 'full'));

      echo select_tag('department', options_for_select($department_list), array('multiple' => true));

      echo select_tag('url', options_for_select($url_list), array('onChange' => 'Javascript:this.form.submit();'));

    returns <select> tag optionally comprised of <option> tags.

  • (string) radiobutton_tag ($name, $value, $checked = false, $options = array())

    $name field name
    $value radio button value (if selected)
    $checked is the radio button selected? (1 or 0)
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;radio&quot;.

    Examples:

      echo ' Yes '.radiobutton_tag('newsletter', 1);
    
    echo ' No '.radiobutton_tag('newsletter', 0);

    returns XHTML compliant <input> tag with type="radio"

  • (string) label_for ($id, $label, $options = array())

    $id id
    $label label or title
    $options additional HTML compliant <label> tag parameters

    Returns a &lt;label&gt; tag with for the specified parameter.

    returns <label> tag with $label for the specified $id parameter.

  • (string) select_country_tag ($name, $selected = null, $options = array())

    $name field name
    $selected selected field value (two-character country code)
    $options additional HTML compliant <select> tag parameters

    Returns a &lt;select&gt; tag populated with all the countries in the world.

    The select_country_tag builds off the traditional select_tag function, and is conveniently populated with all the countries in the world (sorted alphabetically). Each option in the list has a two-character country code for its value and the country's name as its display title. The country data is retrieved via the sfCultureInfo class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world. Here's an example of an <option> tag generated by the select_country_tag:
    <option value="US">United States</option>
    Examples:

      echo select_country_tag('country', 'FR');

    returns <select> tag populated with all the countries in the world.

  • (string) checkbox_tag ($name, $value = &apos;1&apos;, $checked = false, $options = array())

    $name field name
    $value checkbox value (if checked)
    $checked is the checkbox checked? (1 or 0)
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;checkbox&quot;.

    When creating multiple checkboxes with the same name, be sure to use an array for the $name parameter (i.e. 'name[]'). The checkbox_tag is smart enough to create unique ID's based on the $value parameter like so:
    <input type="checkbox" name="status[]" id="status_3" value="3" />
    <input type="checkbox" name="status[]" id="status_4" value="4" />

    Examples:

      echo checkbox_tag('newsletter', 1, $sf_params->get('newsletter'));

      echo checkbox_tag('option_a', 'yes', true, array('class' => 'style_a'));

      // one request variable with an array of checkbox values
    
    echo checkbox_tag('choice[]', 1);
    echo checkbox_tag('choice[]', 2);
    echo checkbox_tag('choice[]', 3);
    echo checkbox_tag('choice[]', 4);

      // assuming you have Prototype.js enabled, you could do this
    
    echo checkbox_tag('show_tos', 1, false, array('onclick' => \"Element.toggle('tos'); return false;\"));

    returns XHTML compliant <input> tag with type="checkbox"

  • (string) get_id_from_name ($name, $value = null)

    $name field name
    $value field value

    Returns a formatted ID based on the parameter and optionally the parameter.

    This function determines the proper form field ID name based on the parameters. If a form field has an array value as a name we need to convert them to proper and unique IDs like so: name[] => name (if value == null)
    name[] => name_value (if value != null)
    name[bob] => name_bob
    name[item][total] => name_item_total

    Examples:

      echo get_id_from_name('status[]', '1');

    returns <select> tag populated with all the languages in the world.

  • (string) input_password_tag ($name = &apos;password&apos;, $value = null, $options = array())

    $name field name
    $value populated field value
    $options additional HTML compliant <input> tag parameters

    Returns an XHTML compliant &lt;input&gt; tag with type=&quot;password&quot;.

    Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize any standard <input> tag parameters passed in the optional $options parameter. The only difference is that it creates the tag with type="password", meaning that the text entered into this field will not be visible to the end user. In most cases it is replaced by * * * * * * * *. Even though this text is not readable, it is recommended that you do not populate the optional $value option with a plain-text password or any other sensitive information, as this is a potential security risk.
    Examples:

      echo input_password_tag('password');
    
    echo input_password_tag('password_confirm');

    returns XHTML compliant <input> tag with type="password"

  • (string) input_date_range_tag ($name, $value, $options = array())

    $name field name
    $value dates: $value&[apos;from'] and $value&[apos;to']
    $options additional HTML compliant <input> tag parameters

    Returns two XHTML compliant &lt;input&gt; tags to be used as a free-text date fields for a date range.

    Built on the input_date_tag, the input_date_range_tag combines two input tags that allow the user to specify a from and to date. You can easily implement a JavaScript calendar by enabling the 'rich' option in the $options parameter. This includes a button next to the field that when clicked, will open an inline JavaScript calendar. When a date is selected, it will automatically populate the <input> tag with the proper date, formatted to the user's culture setting.
    Note: The $name parameter will automatically converted to array names. For example, a $name of "date" becomes date[from] and date[to]
    Options:


    • rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date

    • before - string to be displayed before the input_date_range_tag

    • middle - string to be displayed between the from and to tags

    • after - string to be displayed after the input_date_range_tag



    Examples:
      $date = array('from' => '2006-05-15', 'to' => '2006-06-15');
    
    echo input_date_range_tag('date', $date, array('rich' => true));

      echo input_date_range_tag('date', null, array('middle' => ' through ', 'rich' => true));

    returns XHTML compliant <input> tag with optional JS calendar integration