refactoring the form builder.

This commit is contained in:
Taylor Otwell committed 2013-02-17 22:40:51 -06:00
commit 8e79407d9d
2 files changed
+334

No files matched your search

+297
View File
@@ -0,0 +1,297 @@
<?php namespace Illuminate\Html;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Html\HtmlBuilder as Html;
use Illuminate\Support\Contracts\CsrfTokenProviderInterface;
class FormBuilder {
/**
* The URL generator instance.
*
* @var Illuminate\Routing\UrlGenerator $url
*/
protected $url;
/**
* The CSRF token provider implementation.
*
* @var Illuminate\Support\Contracts\CsrfTokenProviderInterface
*/
protected $tokenProvider;
/**
* The reserved form open attributes.
*
* @var array
*/
protected $reserved = array('method', 'url', 'route', 'action');
/**
* Create a new form builder instance.
*
* @param Illuminate\Routing\UrlGenerator $url
* @return void
*/
public function __construct(UrlGenerator $url,
CsrfTokenProviderInterface $tokenProvider)
{
$this->url = $url;
$this->tokenProvider = $tokenProvider;
}
/**
* Open up a new HTML form.
*
* @param array $options
* @return string
*/
public function open(array $options = array())
{
$method = strtoupper(array_get($options, 'method', 'post'));
// We need to extract the proper method from the attributes. If the method is
// something other than GET or POST we'll use POST since we will spoof the
// actual method since forms don't support PUT or DELETE as native HTML.
$attributes['method'] = $this->getMethod($method);
$attributes['action'] = $this->getAction($options);
$attributes['accept-charset'] = 'UTF-8';
// If the method is PUT or DELETE, we will need to add a spoofer hidden field
// that will instruct this Symfony request to pretend that the method is a
// different method than it actually is, for convenience from the forms.
$append = $this->getAppendage($method);
$attributes = array_merge(
$attributes, array_except($options, $this->reserved)
);
// Finally we're ready to create the final form HTML field. We will attribute
// format the array of attributes. We will also add on the appendage which
// is used to spoof the requests for PUT and DELETE requests to the app.
$attributes = Html::attributes($attributes);
return '<form'.$attributes.'>'.$append;
}
/**
* Generate a hidden field with the current CSRF token.
*
* @return string
*/
public function token()
{
return $this->hidden('csrf_token', $this->tokenProvider->getToken());
}
/**
* Create a form input field.
*
* @param string $type
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function input($type, $name, $value = null, $attributes = array())
{
$attributes['name'] = $name;
$id = $this->getIdAttribute($name, $attributes);
// We will get the appropriate value for the given field. We will look for the
// value in the session for the value in the old input data then we'll look
// in the model instance if one is set. Otherwise we will just use empty.
$value = $this->getValueAttribute($name, $value);
$merge = compact('type', 'value', 'id');
$attributes = array_merge($attributes, $merge);
return '<input'.Html::attributes($attributes).'>';
}
/**
* Create a text input field.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function text($name, $value = null, $attributes = array())
{
return $this->input('text', $name, $value, $attributes);
}
/**
* Create a password input field.
*
* @param string $name
* @param array $attributes
* @return string
*/
public function password($name, $attributes = array())
{
return $this->input('password', $name, '', $attributes);
}
/**
* Create a hidden input field.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function hidden($name, $value, $attributes = array())
{
return $this->input('hidden', $name, $value, $attributes);
}
/**
* Create a file input field.
*
* @param string $name
* @param array $attributes
* @return string
*/
public function file($name, $attributes = array())
{
return $this->input('file', $name, null, $attributes);
}
/**
* Create a textarea input field.
*
* @param string $name
* @param string $value
* @param array $attributes
* @return string
*/
public function textarea($name, $value = null, $attributes = array())
{
$attributes['name'] = $name;
$attributes['id'] = $this->getIdAttribute($name, $attributes);
if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
$value = (string) $this->getValueAttribute($value);
return '<textarea'.Html::attributes($attributes).'>'.e($value).'</textarea>';
}
/**
* Parse the form action method.
*
* @param string $method
* @return string
*/
protected function getMethod($method)
{
return $method != 'GET' ? 'POST' : $method;
}
/**
* Get the form action from the options.
*
* @param array $options
* @return string
*/
protected function getAction(array $options)
{
if (isset($options['url']))
{
return $options['url'];
}
elseif (isset($options['route']))
{
return $this->url->route($options['route']);
}
elseif (isset($options['action']))
{
return $this->url->action($options['action']);
}
return $this->url->current();
}
/**
* Get the form appendage for the given method.
*
* @param string $method
* @return string
*/
protected function getAppendage($method)
{
if ($method == 'PUT' or $method == 'DELETE')
{
$append = $this->hidden('_method', $method);
}
return '';
}
/**
* Get the ID attribute for a field name.
*
* @param string $name
* @param array $attributes
* @return string
*/
protected function getIdAttribute($name, $attributes)
{
if (array_key_exists('id', $attributes))
{
return $attributes['id'];
}
if (in_array($name, $this->labels))
{
return $name;
}
}
/**
* Get the value that should be assigned to the field.
*
* @param string $name
* @param string $value
* @return string
*/
protected function getValueAttribute($name, $value)
{
if ( ! is_null($value)) return $value;
if ($this->session->hasOldInput($name))
{
return $this->session->getOldInput($name);
}
if (isset($this->model) and isset($this->model[$name]))
{
return $this->model[$name];
}
}
/**
* Create a new model based form builder.
*
* @param mixed $model
* @param array $attributes
* @param Closure $callback
* @return void
*/
public function model($model, array $attributes, Closure $callback)
{
//
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php namespace Illuminate\Html;
class Html {
/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
* @return string
*/
public static function attributes($attributes)
{
$html = array();
foreach ((array) $attributes as $key => $value)
{
// For numeric keys we will assume that the key and the value are the same
// as this will convert HTML attributes such as "required" to a correct
// form like required="required" instead of using incorrect numerics.
if (is_numeric($key))
{
$key = $value;
}
// Before we add the attribute, we will make sure the value actually has a
// value so we don't append any null valued attributes onto the list of
// our attributes. We will just skip them over so we do not add them.
if ( ! is_null($value))
{
$html[] = $key.'="'.e($value).'"';
}
}
return count($html) > 0 ? ' '.implode(' ', $html) : '';
}
}