[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> FormHelper.php (sommaire)

FormHelper.

Author: Fabien Potencier <fabien.potencier@symfony-project.com>
Author: David Heinemeier Hansson
Version: SVN: $Id: FormHelper.php 3491 2007-02-18 09:07:59Z fabien $
Poids: 926 lignes (32 kb)
Inclus ou requis:0 fois
Référencé: 0 fois
Nécessite: 0 fichiers

Définit 21 fonctions

  options_for_select()
  form_tag()
  select_tag()
  select_country_tag()
  select_language_tag()
  input_tag()
  input_hidden_tag()
  input_file_tag()
  input_password_tag()
  textarea_tag()
  checkbox_tag()
  radiobutton_tag()
  input_date_range_tag()
  input_date_tag()
  submit_tag()
  reset_tag()
  submit_image_tag()
  label_for()
  get_id_from_name()
  _convert_options()
  _convert_include_custom_for_select()

Fonctions
Fonctions qui ne font pas partie d'une Classe:

options_for_select($options = array()   X-Ref
Returns a formatted set of <option> tags based on optional <i>$options</i> 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 <i>$options</i>, 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.

<b>Options:</b>
- 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

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

<code>
$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 --')));
</code>

<code>
$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'));
</code>

param: array dataset to create <option> tags and <optgroup> tags from
param: string selected option value
param: array  additional HTML compliant <option> tag parameters
return: string populated with <option> tags derived from the <i>$options</i> array variable

form_tag($url_for_options = '', $options = array()   X-Ref
Returns an HTML <form> tag that points to a valid action, route or URL as defined by <i>$url_for_options</i>.

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

<b>Options:</b>
- multipart - When set to true, enctype is set to "multipart/form-data".

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

param: string valid action, route or URL
param: array optional HTML parameters for the <form> tag
return: string opening HTML <form> tag with options

select_tag($name, $option_tags = null, $options = array()   X-Ref
Returns a <select> tag, optionally comprised of <option> tags.

The select tag does not generate <option> tags by default.
To do so, you must populate the <i>$option_tags</i> 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 <i>multiple</i> option to true.
Doing so will automatically convert the name field to an array type variable (i.e. name="name" becomes name="name[]").

<b>Options:</b>
- multiple - If set to true, the select tag will allow multiple options to be selected at once.

<b>Examples:</b>
<code>
$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'));
</code>

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

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

param: string field name
param: mixed contains a string of valid <option></option> tags, or an array of options that will be passed to options_for_select
param: array  additional HTML compliant <select> tag parameters
return: string <select> tag optionally comprised of <option> tags.

select_country_tag($name, $selected = null, $options = array()   X-Ref
Returns a <select> 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:

<samp>
<option value="US">United States</option>
</samp>

<b>Examples:</b>
<code>
echo select_country_tag('country', 'FR');
</code>

param: string field name
param: string selected field value (two-character country code)
param: array  additional HTML compliant <select> tag parameters
return: string <select> tag populated with all the countries in the world.

select_language_tag($name, $selected = null, $options = array()   X-Ref
Returns a <select> 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:

<samp>
<option value="en">English</option>
</samp>

<b>Examples:</b>
<code>
echo select_language_tag('language', 'de');
</code>

param: string field name
param: string selected field value (two or threecharacter language/culture code)
param: array  additional HTML compliant <select> tag parameters
return: string <select> tag populated with all the languages in the world.

input_tag($name, $value = null, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="text".

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

<b>Examples:</b>
<code>
echo input_tag('name');
</code>

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

param: string field name
param: string selected field value
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="text"

input_hidden_tag($name, $value = null, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="hidden".

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 <i>$options</i> parameter.  The only difference is
that it creates the tag with type="hidden", meaning that is not visible on the page.

<b>Examples:</b>
<code>
echo input_hidden_tag('id', $id);
</code>

param: string field name
param: string populated field value
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="hidden"

input_file_tag($name, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="file".

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 <i>$options</i> 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 <i>multipart</i> option form_tag helper function to true
or your files will not be properly uploaded to the web server.

<b>Examples:</b>
<code>
echo input_file_tag('filename', array('size' => 30));
</code>

param: string field name
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="file"

input_password_tag($name = 'password', $value = null, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="password".

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 <i>$options</i> 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 <i>$value</i> option with a plain-text password or any other sensitive information, as this is a
potential security risk.

<b>Examples:</b>
<code>
echo input_password_tag('password');
echo input_password_tag('password_confirm');
</code>

param: string field name
param: string populated field value
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="password"

textarea_tag($name, $content = null, $options = array()   X-Ref
Returns a <textarea> 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 <i>$options</i> 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:

<b>Options:</b>
- rich: A rich text editor class (for example sfRichTextEditorTinyMCE for TinyMCE).

<b>Examples:</b>
<code>
echo textarea_tag('notes');
</code>

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

param: string field name
param: string populated field value
param: array  additional HTML compliant <textarea> tag parameters
return: string <textarea> tag optionally wrapped with a rich-text WYSIWYG editor

checkbox_tag($name, $value = '1', $checked = false, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="checkbox".

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

<samp>
<input type="checkbox" name="status[]" id="status_3" value="3" />
<input type="checkbox" name="status[]" id="status_4" value="4" />
</samp>

<b>Examples:</b>
<code>
echo checkbox_tag('newsletter', 1, $sf_params->get('newsletter'));
</code>

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

<code>
// 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);
</code>

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

param: string field name
param: string checkbox value (if checked)
param: bool   is the checkbox checked? (1 or 0)
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="checkbox"

radiobutton_tag($name, $value, $checked = false, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="radio".

<b>Examples:</b>
<code>
echo ' Yes '.radiobutton_tag('newsletter', 1);
echo ' No '.radiobutton_tag('newsletter', 0);
</code>

param: string field name
param: string radio button value (if selected)
param: bool   is the radio button selected? (1 or 0)
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="radio"

input_date_range_tag($name, $value, $options = array()   X-Ref
Returns two XHTML compliant <input> 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
<i>$options</i> 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.

<b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
For example, a <i>$name</i> of "date" becomes date[from] and date[to]

<b>Options:</b>
- 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

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

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

param: string field name
param: array  dates: $value['from'] and $value['to']
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with optional JS calendar integration

input_date_tag($name, $value = null, $options = array()   X-Ref
Returns an XHTML compliant <input> tag to be used as a free-text date field.

You can easily implement a JavaScript calendar by enabling the 'rich' option in the
<i>$options</i> 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.

<b>Options:</b>
- rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date

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

param: string field name
param: string date
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with optional JS calendar integration

submit_tag($value = 'Save changes', $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="submit".

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 <i>$value</i> parameter
(title of the button) is set to "Save changes", which can be easily overwritten by passing a
<i>$value</i> parameter.

<b>Examples:</b>
<code>
echo submit_tag();
</code>

<code>
echo submit_tag('Update Record');
</code>

param: string field value (title of submit button)
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="submit"

reset_tag($value = 'Reset', $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="reset".

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

<b>Examples:</b>
<code>
echo reset_tag();
</code>

<code>
echo reset_tag('Start Over');
</code>

param: string field value (title of reset button)
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="reset"

submit_image_tag($source, $options = array()   X-Ref
Returns an XHTML compliant <input> tag with type="image".

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
<i>$source</i> 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.

<b>Examples:</b>
<code>
// Assuming your image is in the /web/images/ directory
echo submit_image_tag('my_submit_button.gif');
</code>

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

param: string path to image file
param: array  additional HTML compliant <input> tag parameters
return: string XHTML compliant <input> tag with type="image"

label_for($id, $label, $options = array()   X-Ref
Returns a <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.

param: string id
param: string label or title
param: array  additional HTML compliant <label> tag parameters
return: string <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.

get_id_from_name($name, $value = null)   X-Ref
Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> 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:
<samp>
name[] => name (if value == null)
name[] => name_value (if value != null)
name[bob] => name_bob
name[item][total] => name_item_total
</samp>

<b>Examples:</b>
<code>
echo get_id_from_name('status[]', '1');
</code>

param: string field name
param: string field value
return: string <select> tag populated with all the languages in the world.

_convert_options($options)   X-Ref
Converts specific <i>$options</i> to their correct HTML format

param: array options
return: array returns properly formatted options

_convert_include_custom_for_select($options, &$select_options)   X-Ref
Pas de description



Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7