Commit 80173d48 authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #4 from pedroborges/develop

Added <label> element to form class. Input elements will automatically receive the ID of the corresponding label element if one exists.
parents e87f7dc2 de16d5cf
...@@ -2,6 +2,13 @@ ...@@ -2,6 +2,13 @@
class Form { class Form {
/**
* Stores labels names.
*
* @var array
*/
private static $labels = array();
/** /**
* Open a HTML form. * Open a HTML form.
* *
...@@ -71,6 +78,21 @@ class Form { ...@@ -71,6 +78,21 @@ class Form {
return Session::get('csrf_token'); return Session::get('csrf_token');
} }
/**
* Create a HTML label element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function label($name, $value, $attributes = array())
{
static::$labels[] = $name;
return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
}
/** /**
* Create a HTML text input element. * Create a HTML text input element.
* *
...@@ -190,6 +212,8 @@ class Form { ...@@ -190,6 +212,8 @@ class Form {
$attributes['checked'] = 'checked'; $attributes['checked'] = 'checked';
} }
(in_array($name, static::$labels)) ? $attributes['id'] = $name : null;
return static::input($type, $name, $value, $attributes); return static::input($type, $name, $value, $attributes);
} }
...@@ -204,6 +228,7 @@ class Form { ...@@ -204,6 +228,7 @@ class Form {
public static function textarea($name, $value = '', $attributes = array()) public static function textarea($name, $value = '', $attributes = array())
{ {
$attributes['name'] = $name; $attributes['name'] = $name;
(in_array($name, static::$labels)) ? $attributes['id'] = $name : null;
// ------------------------------------------------------- // -------------------------------------------------------
// Set the default number of rows. // Set the default number of rows.
...@@ -236,6 +261,7 @@ class Form { ...@@ -236,6 +261,7 @@ class Form {
public static function select($name, $options = array(), $selected = null, $attributes = array()) public static function select($name, $options = array(), $selected = null, $attributes = array())
{ {
$attributes['name'] = $name; $attributes['name'] = $name;
(in_array($name, static::$labels)) ? $attributes['id'] = $name : null;
$html_options = array(); $html_options = array();
...@@ -275,6 +301,7 @@ class Form { ...@@ -275,6 +301,7 @@ class Form {
$attributes['type'] = $type; $attributes['type'] = $type;
$attributes['name'] = $name; $attributes['name'] = $name;
$attributes['value'] = $value; $attributes['value'] = $value;
(in_array($name, static::$labels)) ? $attributes['id'] = $name : null;
return '<input'.HTML::attributes($attributes).' />'.PHP_EOL; return '<input'.HTML::attributes($attributes).' />'.PHP_EOL;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment