* // Open a "POST" form to the current request URI * echo Form::open(); * * // Open a "POST" form to a given URI * echo Form::open('user/profile'); * * // Open a "PUT" form to a given URI * echo Form::open('user/profile', 'put'); * * // Open a form that has HTML attributes * echo Form::open('user/profile', 'post', array('class' => 'profile')); * * * @param string $action * @param string $method * @param array $attributes * @param bool $https * @return string */ public static function open($action = null, $method = 'POST', $attributes = array(), $https = null) { $method = strtoupper($method); $attributes['method'] = static::method($method); $attributes['action'] = static::action($action, $https); // If a character encoding has not been specified in the attributes, we will // use the default encoding as specified in the application configuration // file for the "accept-charset" attribute. if ( ! array_key_exists('accept-charset', $attributes)) { $attributes['accept-charset'] = Config::get('application.encoding'); } $append = ''; // Since PUT and DELETE methods are not actually supported by HTML forms, // we'll create a hidden input element that contains the request method // and set the actual request method variable to POST. if ($method == 'PUT' or $method == 'DELETE') { $append = static::hidden(Request::spoofer, $method); } return '
'; } /** * Generate a hidden field containing the current CSRF token. * * @return string */ public static function token() { return static::input('hidden', Session::csrf_token, Session::token()); } /** * Create a HTML label element. * *
* // Create a label for the "email" input element
* echo Form::label('email', 'E-Mail Address');
*
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function label($name, $value, $attributes = array())
{
static::$labels[] = $name;
$attributes = HTML::attributes($attributes);
$value = HTML::entities($value);
return '';
}
/**
* Create a HTML input element.
*
*
* // Create a "text" input element named "email"
* echo Form::input('text', 'email');
*
* // Create an input element with a specified default value
* echo Form::input('text', 'email', 'example@gmail.com');
*
*
* @param string $type
* @param string $name
* @param mixed $value
* @param array $attributes
* @return string
*/
public static function input($type, $name, $value = null, $attributes = array())
{
$name = (isset($attributes['name'])) ? $attributes['name'] : $name;
$id = static::id($name, $attributes);
$attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
return '';
}
/**
* Create a HTML text input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function text($name, $value = null, $attributes = array())
{
return static::input('text', $name, $value, $attributes);
}
/**
* Create a HTML password input element.
*
* @param string $name
* @param array $attributes
* @return string
*/
public static function password($name, $attributes = array())
{
return static::input('password', $name, null, $attributes);
}
/**
* Create a HTML hidden input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function hidden($name, $value = null, $attributes = array())
{
return static::input('hidden', $name, $value, $attributes);
}
/**
* Create a HTML search input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function search($name, $value = null, $attributes = array())
{
return static::input('search', $name, $value, $attributes);
}
/**
* Create a HTML email input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function email($name, $value = null, $attributes = array())
{
return static::input('email', $name, $value, $attributes);
}
/**
* Create a HTML telephone input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function telephone($name, $value = null, $attributes = array())
{
return static::input('tel', $name, $value, $attributes);
}
/**
* Create a HTML URL input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function url($name, $value = null, $attributes = array())
{
return static::input('url', $name, $value, $attributes);
}
/**
* Create a HTML number input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function number($name, $value = null, $attributes = array())
{
return static::input('number', $name, $value, $attributes);
}
/**
* Create a HTML date input element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function date($name, $value = null, $attributes = array())
{
return static::input('date', $name, $value, $attributes);
}
/**
* Create a HTML file input element.
*
* @param string $name
* @param array $attributes
* @return string
*/
public static function file($name, $attributes = array())
{
return static::input('file', $name, null, $attributes);
}
/**
* Create a HTML textarea element.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public static function textarea($name, $value = '', $attributes = array())
{
$attributes['name'] = $name;
$attributes['id'] = static::id($name, $attributes);
if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
return '';
}
/**
* Create a HTML select element.
*
*
* // Create a HTML select element filled with options
* echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
*
* // Create a select element with a default selected value
* echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
*
*
* @param string $name
* @param array $options
* @param string $selected
* @param array $attributes
* @return string
*/
public static function select($name, $options = array(), $selected = null, $attributes = array())
{
$attributes['id'] = static::id($name, $attributes);
$attributes['name'] = $name;
$html = array();
foreach ($options as $value => $display)
{
if (is_array($display))
{
$html[] = static::optgroup($display, $value, $selected);
}
else
{
$html[] = static::option($value, $display, $selected);
}
}
return '';
}
/**
* Create a HTML select element optgroup.
*
* @param array $options
* @param string $label
* @param string $selected
* @return string
*/
protected static function optgroup($options, $label, $selected)
{
$html = array();
foreach ($options as $value => $display)
{
$html[] = static::option($value, $display, $selected);
}
return '