Compare commits

...
5 Commits
Author SHA1 Message Date
Adam Engebretson 59f124084c Merge pull request #63 from rumeau/patch-1
Modify Placeholder to not send data
2015-07-27 22:18:41 -05:00
Jean Rumeau 0f4473f9a0 Update FormBuilderTest.php 2015-07-22 23:33:56 -03:00
Jean Rumeau af7663294a Update FormBuilder.php 2015-07-22 23:30:04 -03:00
Adam Engebretson fb7693cf4b Renaming "optional" to "placeholder" Re: #60
Signed-off-by: Adam Engebretson <adam@enge.me>
2015-07-22 16:00:12 -05:00
Adam Engebretson 9c4fea1f04 Adding Optional Placeholder for Selects Re: #60
Signed-off-by: Adam Engebretson <adam@enge.me>
2015-07-22 14:05:21 -05:00
2 changed files with 171 additions and 79 deletions

No files matched your search

+153 -79
View File
@@ -49,28 +49,28 @@ class FormBuilder {
*
* @var array
*/
protected $labels = array();
protected $labels = [];
/**
* The reserved form open attributes.
*
* @var array
*/
protected $reserved = array('method', 'url', 'route', 'action', 'files');
protected $reserved = ['method', 'url', 'route', 'action', 'files'];
/**
* The form methods that should be spoofed, in uppercase.
*
* @var array
*/
protected $spoofedMethods = array('DELETE', 'PATCH', 'PUT');
protected $spoofedMethods = ['DELETE', 'PATCH', 'PUT'];
/**
* The types of inputs to not fill values on by default.
*
* @var array
*/
protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');
protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
/**
* Create a new form builder instance.
@@ -78,6 +78,7 @@ class FormBuilder {
* @param \Illuminate\Routing\UrlGenerator $url
* @param \Collective\Html\HtmlBuilder $html
* @param string $csrfToken
*
* @return void
*/
public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken)
@@ -91,9 +92,10 @@ class FormBuilder {
* Open up a new HTML form.
*
* @param array $options
*
* @return string
*/
public function open(array $options = array())
public function open(array $options = [])
{
$method = array_get($options, 'method', 'post');
@@ -111,7 +113,7 @@ class FormBuilder {
// different method than it actually is, for convenience from the forms.
$append = $this->getAppendage($method);
if (isset($options['files']) && $options['files'])
if(isset($options['files']) && $options['files'])
{
$options['enctype'] = 'multipart/form-data';
}
@@ -130,7 +132,7 @@ class FormBuilder {
// extra value for the hidden _method field if it's needed for the form.
$attributes = $this->html->attributes($attributes);
return '<form'.$attributes.'>'.$append;
return '<form' . $attributes . '>' . $append;
}
/**
@@ -138,9 +140,10 @@ class FormBuilder {
*
* @param mixed $model
* @param array $options
*
* @return string
*/
public function model($model, array $options = array())
public function model($model, array $options = [])
{
$this->model = $model;
@@ -151,6 +154,7 @@ class FormBuilder {
* Set the model instance on the form builder.
*
* @param mixed $model
*
* @return void
*/
public function setModel($model)
@@ -165,7 +169,7 @@ class FormBuilder {
*/
public function close()
{
$this->labels = array();
$this->labels = [];
$this->model = null;
@@ -190,9 +194,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function label($name, $value = null, $options = array())
public function label($name, $value = null, $options = [])
{
$this->labels[] = $name;
@@ -200,7 +205,7 @@ class FormBuilder {
$value = e($this->formatLabel($name, $value));
return '<label for="'.$name.'"'.$options.'>'.$value.'</label>';
return '<label for="' . $name . '"' . $options . '>' . $value . '</label>';
}
/**
@@ -208,6 +213,7 @@ class FormBuilder {
*
* @param string $name
* @param string|null $value
*
* @return string
*/
protected function formatLabel($name, $value)
@@ -222,18 +228,19 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function input($type, $name, $value = null, $options = array())
public function input($type, $name, $value = null, $options = [])
{
if ( ! isset($options['name'])) $options['name'] = $name;
if( ! isset($options['name'])) $options['name'] = $name;
// 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.
$id = $this->getIdAttribute($name, $options);
if ( ! in_array($type, $this->skipValueTypes))
if( ! in_array($type, $this->skipValueTypes))
{
$value = $this->getValueAttribute($name, $value);
}
@@ -245,7 +252,7 @@ class FormBuilder {
$options = array_merge($options, $merge);
return '<input'.$this->html->attributes($options).'>';
return '<input' . $this->html->attributes($options) . '>';
}
/**
@@ -254,9 +261,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function text($name, $value = null, $options = array())
public function text($name, $value = null, $options = [])
{
return $this->input('text', $name, $value, $options);
}
@@ -266,9 +274,10 @@ class FormBuilder {
*
* @param string $name
* @param array $options
*
* @return string
*/
public function password($name, $options = array())
public function password($name, $options = [])
{
return $this->input('password', $name, '', $options);
}
@@ -279,9 +288,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function hidden($name, $value = null, $options = array())
public function hidden($name, $value = null, $options = [])
{
return $this->input('hidden', $name, $value, $options);
}
@@ -292,9 +302,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function email($name, $value = null, $options = array())
public function email($name, $value = null, $options = [])
{
return $this->input('email', $name, $value, $options);
}
@@ -305,9 +316,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function tel($name, $value = null, $options = array())
public function tel($name, $value = null, $options = [])
{
return $this->input('tel', $name, $value, $options);
}
@@ -318,9 +330,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function number($name, $value = null, $options = array())
public function number($name, $value = null, $options = [])
{
return $this->input('number', $name, $value, $options);
}
@@ -331,11 +344,12 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function date($name, $value = null, $options = array())
public function date($name, $value = null, $options = [])
{
if ($value instanceof DateTime)
if($value instanceof DateTime)
{
$value = $value->format('Y-m-d');
}
@@ -349,9 +363,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function time($name, $value = null, $options = array())
public function time($name, $value = null, $options = [])
{
return $this->input('time', $name, $value, $options);
}
@@ -362,9 +377,10 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function url($name, $value = null, $options = array())
public function url($name, $value = null, $options = [])
{
return $this->input('url', $name, $value, $options);
}
@@ -374,9 +390,10 @@ class FormBuilder {
*
* @param string $name
* @param array $options
*
* @return string
*/
public function file($name, $options = array())
public function file($name, $options = [])
{
return $this->input('file', $name, null, $options);
}
@@ -387,11 +404,12 @@ class FormBuilder {
* @param string $name
* @param string $value
* @param array $options
*
* @return string
*/
public function textarea($name, $value = null, $options = array())
public function textarea($name, $value = null, $options = [])
{
if ( ! isset($options['name'])) $options['name'] = $name;
if( ! isset($options['name'])) $options['name'] = $name;
// Next we will look for the rows and cols attributes, as each of these are put
// on the textarea element definition. If they are not present, we will just
@@ -409,18 +427,19 @@ class FormBuilder {
// the element. Then we'll create the final textarea elements HTML for us.
$options = $this->html->attributes($options);
return '<textarea'.$options.'>'.e($value).'</textarea>';
return '<textarea' . $options . '>' . e($value) . '</textarea>';
}
/**
* Set the text area size on the attributes.
*
* @param array $options
*
* @return array
*/
protected function setTextAreaSize($options)
{
if (isset($options['size']))
if(isset($options['size']))
{
return $this->setQuickTextAreaSize($options);
}
@@ -439,13 +458,14 @@ class FormBuilder {
* Set the text area size using the quick "size" attribute.
*
* @param array $options
*
* @return array
*/
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
return array_merge($options, ['cols' => $segments[0], 'rows' => $segments[1]]);
}
/**
@@ -455,9 +475,10 @@ class FormBuilder {
* @param array $list
* @param string $selected
* @param array $options
*
* @return string
*/
public function select($name, $list = array(), $selected = null, $options = array())
public function select($name, $list = [], $selected = null, $options = [])
{
// When building a select box the "value" attribute is really the selected one
// so we will use that when checking the model or session for a value which
@@ -466,14 +487,20 @@ class FormBuilder {
$options['id'] = $this->getIdAttribute($name, $options);
if ( ! isset($options['name'])) $options['name'] = $name;
if( ! isset($options['name'])) $options['name'] = $name;
// We will simply loop through the options and build an HTML value for each of
// them until we have an array of HTML declarations. Then we will join them
// all together into one single HTML element that can be put on the form.
$html = array();
$html = [];
foreach ($list as $value => $display)
if(isset($options['placeholder']))
{
$html[] = $this->placeholderOption($options['placeholder'], $selected);
unset($options['placeholder']);
}
foreach($list as $value => $display)
{
$html[] = $this->getSelectOption($display, $value, $selected);
}
@@ -496,9 +523,10 @@ class FormBuilder {
* @param string $end
* @param string $selected
* @param array $options
*
* @return string
*/
public function selectRange($name, $begin, $end, $selected = null, $options = array())
public function selectRange($name, $begin, $end, $selected = null, $options = [])
{
$range = array_combine($range = range($begin, $end), $range);
@@ -513,11 +541,12 @@ class FormBuilder {
* @param string $end
* @param string $selected
* @param array $options
*
* @return string
*/
public function selectYear()
{
return call_user_func_array(array($this, 'selectRange'), func_get_args());
return call_user_func_array([$this, 'selectRange'], func_get_args());
}
/**
@@ -527,13 +556,14 @@ class FormBuilder {
* @param string $selected
* @param array $options
* @param string $format
*
* @return string
*/
public function selectMonth($name, $selected = null, $options = array(), $format = '%B')
public function selectMonth($name, $selected = null, $options = [], $format = '%B')
{
$months = array();
$months = [];
foreach (range(1, 12) as $month)
foreach(range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
@@ -547,11 +577,12 @@ class FormBuilder {
* @param string $display
* @param string $value
* @param string $selected
*
* @return string
*/
public function getSelectOption($display, $value, $selected)
{
if (is_array($display))
if(is_array($display))
{
return $this->optionGroup($display, $value, $selected);
}
@@ -565,18 +596,19 @@ class FormBuilder {
* @param array $list
* @param string $label
* @param string $selected
*
* @return string
*/
protected function optionGroup($list, $label, $selected)
{
$html = array();
$html = [];
foreach ($list as $value => $display)
foreach($list as $value => $display)
{
$html[] = $this->option($display, $value, $selected);
}
return '<optgroup label="'.e($label).'">'.implode('', $html).'</optgroup>';
return '<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>';
}
/**
@@ -585,15 +617,34 @@ class FormBuilder {
* @param string $display
* @param string $value
* @param string $selected
*
* @return string
*/
protected function option($display, $value, $selected)
{
$selected = $this->getSelectedValue($value, $selected);
$options = array('value' => e($value), 'selected' => $selected);
$options = ['value' => e($value), 'selected' => $selected];
return '<option'.$this->html->attributes($options).'>'.e($display).'</option>';
return '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
}
/**
* Create a placeholder select element option.
*
* @param $display
* @param $selected
*
* @return string
*/
protected function placeholderOption($display, $selected)
{
$selected = $this->getSelectedValue(null, $selected);
$options = compact('selected');
$options['value'] = '';
return '<option' . $this->html->attributes($options) . '>' . e($display) . '</option>';
}
/**
@@ -601,11 +652,12 @@ class FormBuilder {
*
* @param string $value
* @param string $selected
*
* @return string|null
*/
protected function getSelectedValue($value, $selected)
{
if (is_array($selected))
if(is_array($selected))
{
return in_array($value, $selected) ? 'selected' : null;
}
@@ -620,9 +672,10 @@ class FormBuilder {
* @param mixed $value
* @param bool $checked
* @param array $options
*
* @return string
*/
public function checkbox($name, $value = 1, $checked = null, $options = array())
public function checkbox($name, $value = 1, $checked = null, $options = [])
{
return $this->checkable('checkbox', $name, $value, $checked, $options);
}
@@ -634,11 +687,12 @@ class FormBuilder {
* @param mixed $value
* @param bool $checked
* @param array $options
*
* @return string
*/
public function radio($name, $value = null, $checked = null, $options = array())
public function radio($name, $value = null, $checked = null, $options = [])
{
if (is_null($value)) $value = $name;
if(is_null($value)) $value = $name;
return $this->checkable('radio', $name, $value, $checked, $options);
}
@@ -651,13 +705,14 @@ class FormBuilder {
* @param mixed $value
* @param bool $checked
* @param array $options
*
* @return string
*/
protected function checkable($type, $name, $value, $checked, $options)
{
$checked = $this->getCheckedState($type, $name, $value, $checked);
if ($checked) $options['checked'] = 'checked';
if($checked) $options['checked'] = 'checked';
return $this->input($type, $name, $value, $options);
}
@@ -669,11 +724,12 @@ class FormBuilder {
* @param string $name
* @param mixed $value
* @param bool $checked
*
* @return bool
*/
protected function getCheckedState($type, $name, $value, $checked)
{
switch ($type)
switch($type)
{
case 'checkbox':
return $this->getCheckboxCheckedState($name, $value, $checked);
@@ -692,13 +748,14 @@ class FormBuilder {
* @param string $name
* @param mixed $value
* @param bool $checked
*
* @return bool
*/
protected function getCheckboxCheckedState($name, $value, $checked)
{
if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) return false;
if(isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) return false;
if ($this->missingOldAndModel($name)) return $checked;
if($this->missingOldAndModel($name)) return $checked;
$posted = $this->getValueAttribute($name);
@@ -711,11 +768,12 @@ class FormBuilder {
* @param string $name
* @param mixed $value
* @param bool $checked
*
* @return bool
*/
protected function getRadioCheckedState($name, $value, $checked)
{
if ($this->missingOldAndModel($name)) return $checked;
if($this->missingOldAndModel($name)) return $checked;
return $this->getValueAttribute($name) == $value;
}
@@ -724,6 +782,7 @@ class FormBuilder {
* Determine if old input or model input exists for a key.
*
* @param string $name
*
* @return bool
*/
protected function missingOldAndModel($name)
@@ -736,9 +795,10 @@ class FormBuilder {
*
* @param string $value
* @param array $attributes
*
* @return string
*/
public function reset($value, $attributes = array())
public function reset($value, $attributes = [])
{
return $this->input('reset', null, $value, $attributes);
}
@@ -749,9 +809,10 @@ class FormBuilder {
* @param string $url
* @param string $name
* @param array $attributes
*
* @return string
*/
public function image($url, $name = null, $attributes = array())
public function image($url, $name = null, $attributes = [])
{
$attributes['src'] = $this->url->asset($url);
@@ -763,9 +824,10 @@ class FormBuilder {
*
* @param string $value
* @param array $options
*
* @return string
*/
public function submit($value = null, $options = array())
public function submit($value = null, $options = [])
{
return $this->input('submit', null, $value, $options);
}
@@ -775,22 +837,24 @@ class FormBuilder {
*
* @param string $value
* @param array $options
*
* @return string
*/
public function button($value = null, $options = array())
public function button($value = null, $options = [])
{
if ( ! array_key_exists('type', $options))
if( ! array_key_exists('type', $options))
{
$options['type'] = 'button';
}
return '<button'.$this->html->attributes($options).'>'.$value.'</button>';
return '<button' . $this->html->attributes($options) . '>' . $value . '</button>';
}
/**
* Parse the form action method.
*
* @param string $method
*
* @return string
*/
protected function getMethod($method)
@@ -804,6 +868,7 @@ class FormBuilder {
* Get the form action from the options.
*
* @param array $options
*
* @return string
*/
protected function getAction(array $options)
@@ -811,12 +876,12 @@ class FormBuilder {
// We will also check for a "route" or "action" parameter on the array so that
// developers can easily specify a route or controller action when creating
// a form providing a convenient interface for creating the form actions.
if (isset($options['url']))
if(isset($options['url']))
{
return $this->getUrlAction($options['url']);
}
if (isset($options['route']))
if(isset($options['route']))
{
return $this->getRouteAction($options['route']);
}
@@ -824,7 +889,7 @@ class FormBuilder {
// If an action is available, we are attempting to open a form to a controller
// action route. So, we will use the URL generator to get the path to these
// actions and return them from the method. Otherwise, we'll use current.
elseif (isset($options['action']))
elseif(isset($options['action']))
{
return $this->getControllerAction($options['action']);
}
@@ -836,11 +901,12 @@ class FormBuilder {
* Get the action for a "url" option.
*
* @param array|string $options
*
* @return string
*/
protected function getUrlAction($options)
{
if (is_array($options))
if(is_array($options))
{
return $this->url->to($options[0], array_slice($options, 1));
}
@@ -852,11 +918,12 @@ class FormBuilder {
* Get the action for a "route" option.
*
* @param array|string $options
*
* @return string
*/
protected function getRouteAction($options)
{
if (is_array($options))
if(is_array($options))
{
return $this->url->route($options[0], array_slice($options, 1));
}
@@ -868,11 +935,12 @@ class FormBuilder {
* Get the action for an "action" option.
*
* @param array|string $options
*
* @return string
*/
protected function getControllerAction($options)
{
if (is_array($options))
if(is_array($options))
{
return $this->url->action($options[0], array_slice($options, 1));
}
@@ -884,16 +952,17 @@ class FormBuilder {
* Get the form appendage for the given method.
*
* @param string $method
*
* @return string
*/
protected function getAppendage($method)
{
list($method, $appendage) = array(strtoupper($method), '');
list($method, $appendage) = [strtoupper($method), ''];
// If the HTTP method is in this list of spoofed methods, we will attach the
// method spoofer hidden input to the form. This allows us to use regular
// form to initiate PUT and DELETE requests in addition to the typical.
if (in_array($method, $this->spoofedMethods))
if(in_array($method, $this->spoofedMethods))
{
$appendage .= $this->hidden('_method', $method);
}
@@ -901,7 +970,7 @@ class FormBuilder {
// If the method is something other than GET we will go ahead and attach the
// CSRF token to the form, as this can't hurt and is convenient to simply
// always have available on every form the developers creates for them.
if ($method != 'GET')
if($method != 'GET')
{
$appendage .= $this->token();
}
@@ -914,16 +983,17 @@ class FormBuilder {
*
* @param string $name
* @param array $attributes
*
* @return string
*/
public function getIdAttribute($name, $attributes)
{
if (array_key_exists('id', $attributes))
if(array_key_exists('id', $attributes))
{
return $attributes['id'];
}
if (in_array($name, $this->labels))
if(in_array($name, $this->labels))
{
return $name;
}
@@ -934,20 +1004,21 @@ class FormBuilder {
*
* @param string $name
* @param string $value
*
* @return mixed
*/
public function getValueAttribute($name, $value = null)
{
if (is_null($name)) return $value;
if(is_null($name)) return $value;
if ( ! is_null($this->old($name)))
if( ! is_null($this->old($name)))
{
return $this->old($name);
}
if ( ! is_null($value)) return $value;
if( ! is_null($value)) return $value;
if (isset($this->model))
if(isset($this->model))
{
return $this->getModelValueAttribute($name);
}
@@ -957,6 +1028,7 @@ class FormBuilder {
* Get the model value that should be assigned to the field.
*
* @param string $name
*
* @return mixed
*/
protected function getModelValueAttribute($name)
@@ -968,11 +1040,12 @@ class FormBuilder {
* Get a value from the session's old input.
*
* @param string $name
*
* @return mixed
*/
public function old($name)
{
if (isset($this->session))
if(isset($this->session))
{
return $this->session->getOldInput($this->transformKey($name));
}
@@ -992,11 +1065,12 @@ class FormBuilder {
* Transform key from array to dot syntax.
*
* @param string $key
*
* @return string
*/
protected function transformKey($key)
{
return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key);
return str_replace(['.', '[]', '[', ']'], ['_', '', '.', ''], $key);
}
/**
@@ -1013,6 +1087,7 @@ class FormBuilder {
* Set the session store implementation.
*
* @param \Illuminate\Session\Store $session
*
* @return $this
*/
public function setSessionStore(Session $session)
@@ -1021,5 +1096,4 @@ class FormBuilder {
return $this;
}
}
+18
View File
@@ -314,6 +314,24 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($select, '<select name="size[key]"><option value="L">Large</option><option value="M">Medium</option><option value="S" selected="selected">Small</option></select>');
}
public function testFormWithOptionalPlaceholder()
{
$select = $this->formBuilder->select(
'size',
['L' => 'Large', 'S' => 'Small'],
null,
['placeholder' => 'Select One...']
);
$this->assertEquals($select, '<select name="size"><option selected="selected" value="">Select One...</option><option value="L">Large</option><option value="S">Small</option></select>');
$select = $this->formBuilder->select(
'size',
['L' => 'Large', 'S' => 'Small'],
'L',
['placeholder' => 'Select One...']
);
$this->assertEquals($select, '<select name="size"><option value="">Select One...</option><option value="L" selected="selected">Large</option><option value="S">Small</option></select>');
}
public function testFormSelectYear()
{