Cleaned up form helper, massively cutting down on unnecessary code and making the method names reflect the input type (eg: dropdown() is now called select())

This commit is contained in:
Woody Gilk 2009-05-31 17:42:02 -05:00
parent 64f1ca7e2a
commit dfc2f3339c

View file

@ -12,451 +12,321 @@ class form_Core {
/** /**
* Generates an opening HTML form tag. * Generates an opening HTML form tag.
* *
* @param string form action attribute * @param string form action
* @param array extra attributes * @param array html attributes
* @param array hidden fields to be created immediately after the form tag
* @return string * @return string
*/ */
public static function open($action = NULL, $attr = array(), $hidden = NULL) public static function open($action = NULL, array $attributes = NULL)
{ {
// Make sure that the method is always set
empty($attr['method']) and $attr['method'] = 'post';
if ($attr['method'] !== 'post' AND $attr['method'] !== 'get')
{
// If the method is invalid, use post
$attr['method'] = 'post';
}
if ($action === NULL) if ($action === NULL)
{ {
// Use the current URL as the default action // Use the current URI
$action = url::site(Router::$complete_uri); $action = Request::instance()->uri;
}
if ($action === '')
{
// Use only the base URI
$action = Kohana::$base_url;
} }
elseif (strpos($action, '://') === FALSE) elseif (strpos($action, '://') === FALSE)
{ {
// Make the action URI into a URL // Make the URI absolute
$action = url::site($action); $action = url::site($action);
} }
// Set action // Add the form action to the attributes
$attr['action'] = $action; $attributes['action'] = $action;
// Only accept the default character set // Only accept the default character set
$attr['accept-charset'] = Kohana::$charset; $attributes['accept-charset'] = Kohana::$charset;
// Form opening tag if ( ! isset($attributes['method']))
$form = '<form'.html::attributes($attr).'>'."\n"; {
// Use POST method
$attributes['method'] = 'post';
}
// Add hidden fields immediate after opening tag return '<form'.html::attributes($attributes).'>';
empty($hidden) or $form .= form::hidden($hidden);
return $form;
} }
/** /**
* Generates an opening HTML form tag that can be used for uploading files. * Creates the closing form tag.
* *
* @param string form action attribute
* @param array extra attributes
* @param array hidden fields to be created immediately after the form tag
* @return string * @return string
*/ */
public static function open_multipart($action = NULL, $attr = array(), $hidden = array()) public static function close()
{ {
// Set multi-part form type return '</form>';
$attr['enctype'] = 'multipart/form-data';
return form::open($action, $attr, $hidden);
} }
/** /**
* Generates a fieldset opening tag. * Creates a form input. If no type is specified, a "text" type input will
* be returned.
* *
* @param string input name
* @param string input value
* @param array html attributes * @param array html attributes
* @param string a string to be attached to the end of the attributes
* @return string * @return string
*/ */
public static function open_fieldset($data = NULL, $extra = '') public static function input($name, $value = '', array $attributes = NULL)
{ {
return '<fieldset'.html::attributes((array) $data).' '.$extra.'>'."\n"; // Set the input name
$attributes['name'] = $name;
// Set the input value
$attributes['value'] = $value;
if ( ! isset($attributes['type']))
{
// Default type is text
$attributes['type'] = 'text';
}
return '<input'.html::attributes($attributes).' />';
} }
/** /**
* Generates a fieldset closing tag. * Creates a hidden form input.
* *
* @param string input name
* @param string input value
* @param array html attributes
* @return string * @return string
*/ */
public static function close_fieldset() public static function hidden($name, $value = '', array $attributes = NULL)
{ {
return '</fieldset>'."\n"; $attributes['type'] = 'hidden';
return form::input($name, $value, $attributes);
} }
/** /**
* Generates a legend tag for use with a fieldset. * Creates a password form input.
* *
* @param string legend text * @param string input name
* @param array HTML attributes * @param string input value
* @param string a string to be attached to the end of the attributes * @param array html attributes
* @return string * @return string
*/ */
public static function legend($text = '', $data = NULL, $extra = '') public static function password($name, $value = '', array $attributes = NULL)
{ {
return '<legend'.html::attributes((array) $data).' '.$extra.'>'.$text.'</legend>'."\n"; $attributes['type'] = 'password';
return form::input($name, $value, $attributes);
} }
/** /**
* Generates hidden form fields. * Creates a file upload form input.
* You can pass a simple key/value string or an associative array with multiple values.
* *
* @param string|array input name (string) or key/value pairs (array) * @param string input name
* @param string input value, if using an input name * @param string input value
* @param array html attributes
* @return string * @return string
*/ */
public static function hidden($data, $value = '') public static function file($name, $value = '', array $attributes = NULL)
{ {
if ( ! is_array($data)) $attributes['type'] = 'file';
{
$data = array
(
$data => $value
);
}
$input = ''; return form::input($name, $value, $attributes);
foreach ($data as $name => $value)
{
$attr = array
(
'type' => 'hidden',
'name' => $name,
'value' => $value
);
$input .= form::input($attr)."\n";
}
return $input;
} }
/** /**
* Creates an HTML form input tag. Defaults to a text type. * Creates a checkbox form input.
* *
* @param string|array input name or an array of HTML attributes * @param string input name
* @param string input value, when using a name * @param string input value
* @param string a string to be attached to the end of the attributes * @param boolean checked status
* @param array html attributes
* @return string * @return string
*/ */
public static function input($data, $value = '', $extra = '') public static function checkbox($name, $value = '', $checked = FALSE, array $attributes = NULL)
{ {
if ( ! is_array($data)) $attributes['type'] = 'checkbox';
if ($checked === TRUE)
{ {
$data = array('name' => $data); // Make the checkbox active
$attributes['checked'] = 'checked';
} }
// Type and value are required attributes return form::input($name, $value, $attributes);
$data += array
(
'type' => 'text',
'value' => $value
);
return '<input'.html::attributes($data).' '.$extra.' />';
} }
/** /**
* Creates a HTML form password input tag. * Creates a radio form input.
* *
* @param string|array input name or an array of HTML attributes * @param string input name
* @param string input value, when using a name * @param string input value
* @param string a string to be attached to the end of the attributes * @param boolean checked status
* @param array html attributes
* @return string * @return string
*/ */
public static function password($data, $value = '', $extra = '') public static function radio($name, $value = '', $checked = FALSE, array $attributes = NULL)
{ {
if ( ! is_array($data)) $attributes['type'] = 'radio';
if ($checked === TRUE)
{ {
$data = array('name' => $data); // Make the radio active
$attributes['checked'] = 'checked';
} }
$data['type'] = 'password'; return form::input($name, $value, $attributes);
return form::input($data, $value, $extra);
} }
/** /**
* Creates an HTML form upload input tag. * Creates a textarea form input.
* *
* @param string|array input name or an array of HTML attributes * @param string textarea name
* @param string input value, when using a name * @param string textarea body
* @param string a string to be attached to the end of the attributes * @param array html attributes
* @param boolean encode existing HTML characters
* @return string * @return string
*/ */
public static function upload($data, $value = '', $extra = '') public static function textarea($name, $body = '', array $attributes = NULL, $double_encode = TRUE)
{ {
if ( ! is_array($data)) // Set the input name
{ $attributes['name'] = $name;
$data = array('name' => $data);
}
$data['type'] = 'file'; // Make the textarea body HTML-safe
$body = htmlspecialchars($title, ENT_NOQUOTES, Kohana::$charset, $double_encode);
return form::input($data, $value, $extra); return '<textarea'.html::attributes($attributes).'>'.$body.'</textarea>';
} }
/** /**
* Creates an HTML form textarea tag. * Creates a select form input.
* *
* @param string|array input name or an array of HTML attributes * @param string input name
* @param string input value, when using a name * @param array available options
* @param string a string to be attached to the end of the attributes * @param string selected option
* @param boolean encode existing entities * @param array html attributes
* @return string * @return string
*/ */
public static function textarea($data, $value = '', $extra = '', $double_encode = TRUE) public static function select($name, array $options = NULL, $selected = NULL, array $attributes = NULL)
{ {
if ( ! is_array($data)) // Set the input name
$attributes['name'] = $name;
if (empty($options))
{ {
$data = array('name' => $data); // There are no options
} $options = '';
// Use the value from $data if possible, or use $value
$value = isset($data['value']) ? $data['value'] : $value;
// Value is not part of the attributes
unset($data['value']);
return '<textarea'.html::attributes($data, 'textarea').' '.$extra.'>'.html::specialchars($value, $double_encode).'</textarea>';
}
/**
* Creates an HTML form select tag, or "dropdown menu".
*
* @param string|array input name or an array of HTML attributes
* @param array select options, when using a name
* @param string option key that should be selected by default
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function dropdown($data, $options = NULL, $selected = NULL, $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
} }
else else
{ {
if (isset($data['options'])) foreach ($options as $value => $name)
{ {
// Use data options if (is_array($name))
$options = $data['options']; {
// Create a new optgroup
$group = array('label' => $value);
// Create a new list of options
$_options = array();
foreach ($name as $_value => $_name)
{
// Create a new attribute set for this option
$option = array('value' => $_value);
if ($_value === $selected)
{
// This option is selected
$option['selected'] = 'selected';
} }
if (isset($data['selected'])) // Sanitize the option title
{ $title = htmlspecialchars($_name, ENT_NOQUOTES, Kohana::$charset, FALSE);
// Use data selected
$selected = $data['selected']; // Change the option to the HTML string
} $_options[] = '<option'.html::attributes($option).'>'.$title.'</option>';
} }
if (is_array($selected)) // Compile the options into a string
{ $_options = "\n".implode("\n", $_options)."\n";
// Multi-select box
$data['multiple'] = 'multiple'; $options[$value] = '<optgroup'.html::attributes($group).'>'.$_options.'</optgroup>';
} }
else else
{ {
// Single selection (but converted to an array) // Create a new attribute set for this option
$selected = array($selected); $option = array('value' => $value);
if ($value === $selected)
{
// This option is selected
$option['selected'] = 'selected';
} }
$input = '<select'.html::attributes($data, 'select').' '.$extra.'>'."\n"; // Sanitize the option title
foreach ((array) $options as $key => $val) $title = htmlspecialchars($name, ENT_NOQUOTES, Kohana::$charset, FALSE);
{
// Key should always be a string
$key = (string) $key;
if (is_array($val)) // Change the option to the HTML string
{ $options[$value] = '<option'.html::attributes($option).'>'.$title.'</option>';
$input .= '<optgroup label="'.$key.'">'."\n"; }
foreach ($val as $inner_key => $inner_val) }
{
// Inner key should always be a string
$inner_key = (string) $inner_key;
$sel = in_array($inner_key, $selected) ? ' selected="selected"' : ''; // Compile the options into a single string
$input .= '<option value="'.$inner_key.'"'.$sel.'>'.$inner_val.'</option>'."\n"; $options = "\n".implode("\n", $options)."\n";
} }
$input .= '</optgroup>'."\n";
}
else
{
$sel = in_array($key, $selected) ? ' selected="selected"' : '';
$input .= '<option value="'.$key.'"'.$sel.'>'.$val.'</option>'."\n";
}
}
$input .= '</select>';
return $input; return '<select'.html::attributes($attributes).'>'.$options.'</select>';
} }
/** /**
* Creates an HTML form checkbox input tag. * Creates a submit form input.
* *
* @param string|array input name or an array of HTML attributes * @param string input name
* @param string input value, when using a name * @param string input value
* @param boolean make the checkbox checked by default * @param array html attributes
* @param string a string to be attached to the end of the attributes
* @return string * @return string
*/ */
public static function checkbox($data, $value = '', $checked = FALSE, $extra = '') public static function submit($name, $value = '', array $attributes = NULL)
{ {
if ( ! is_array($data)) $attributes['type'] = 'submit';
{
$data = array('name' => $data);
}
$data['type'] = 'checkbox'; return form::input($name, $value, $attributes);
if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
{
$data['checked'] = 'checked';
}
else
{
unset($data['checked']);
}
return form::input($data, $value, $extra);
} }
/** /**
* Creates an HTML form radio input tag. * Creates a button form input. Note that the body of a button is NOT escaped,
* to allow images and other HTML to be used.
* *
* @param string|array input name or an array of HTML attributes * @param string input name
* @param string input value, when using a name * @param string input value
* @param boolean make the radio selected by default * @param array html attributes
* @param string a string to be attached to the end of the attributes
* @return string * @return string
*/ */
public static function radio($data = '', $value = '', $checked = FALSE, $extra = '') public static function button($name, $body, array $attributes = NULL)
{ {
if ( ! is_array($data)) // Set the input name
{ $attributes['name'] = $name;
$data = array('name' => $data);
}
$data['type'] = 'radio'; return '<button'.html::attributes($attributes).'>'.$body.'</button>';
if ($checked == TRUE OR (isset($data['checked']) AND $data['checked'] == TRUE))
{
$data['checked'] = 'checked';
}
else
{
unset($data['checked']);
}
return form::input($data, $value, $extra);
} }
/** /**
* Creates an HTML form submit input tag. * Creates a form label.
* *
* @param string|array input name or an array of HTML attributes * @param string target input
* @param string input value, when using a name * @param string label text
* @param string a string to be attached to the end of the attributes * @param array html attributes
* @return string * @return string
*/ */
public static function submit($data = '', $value = '', $extra = '') public static function label($input, $text = NULL, array $attributes = NULL)
{ {
if ( ! is_array($data)) if ($text === NULL)
{ {
$data = array('name' => $data); // Use the input name as the text
$text = $input;
} }
if (empty($data['name'])) // Set the label target
{ $attributes['for'] = $input;
// Remove the name if it is empty
unset($data['name']);
}
$data['type'] = 'submit'; return '<label'.html::attributes($attributes).'>'.$text.'</label>';
return form::input($data, $value, $extra);
}
/**
* Creates an HTML form button input tag.
*
* @param string|array input name or an array of HTML attributes
* @param string input value, when using a name
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function button($data = '', $value = '', $extra = '')
{
if ( ! is_array($data))
{
$data = array('name' => $data);
}
if (empty($data['name']))
{
// Remove the name if it is empty
unset($data['name']);
}
if (isset($data['value']) AND empty($value))
{
$value = arr::remove('value', $data);
}
return '<button'.html::attributes($data, 'button').' '.$extra.'>'.$value.'</button>';
}
/**
* Closes an open form tag.
*
* @param string string to be attached after the closing tag
* @return string
*/
public static function close($extra = '')
{
return '</form>'."\n".$extra;
}
/**
* Creates an HTML form label tag.
*
* @param string|array label "for" name or an array of HTML attributes
* @param string label text or HTML
* @param string a string to be attached to the end of the attributes
* @return string
*/
public static function label($data = '', $text = NULL, $extra = '')
{
if ( ! is_array($data))
{
if (is_string($data))
{
// Specify the input this label is for
$data = array('for' => $data);
}
else
{
// No input specified
$data = array();
}
}
if ($text === NULL AND isset($data['for']))
{
// Make the text the human-readable input name
$text = ucwords(inflector::humanize($data['for']));
}
return '<label'.html::attributes($data).' '.$extra.'>'.$text.'</label>';
} }
} // End form } // End form