Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b7c357e78 | ||
|
|
7ea28eea5e | ||
|
|
3ff2b89ea4 | ||
|
|
7c02a4f0fd | ||
|
|
3617dac0c4 | ||
|
|
de73798e3e | ||
|
|
59f124084c | ||
|
|
0f4473f9a0 | ||
|
|
af7663294a | ||
|
|
fb7693cf4b | ||
|
|
9c4fea1f04 | ||
|
|
c93974c835 | ||
|
|
501aea5374 | ||
|
|
72d6ea9713 | ||
|
|
5edfe5d8bb | ||
|
|
42a626c23e | ||
|
|
0918dac79f | ||
|
|
28a91965be | ||
|
|
f6d0f1c5c4 | ||
|
|
f1eddc7a78 | ||
|
|
745c10226d | ||
|
|
ebe5123467 | ||
|
|
a0a763779d | ||
|
|
c55fda58b1 | ||
|
|
8ae4aa0627 | ||
|
|
4edbd282ba | ||
|
|
3166c19203 | ||
|
|
daae1062da | ||
|
|
e4b32919a3 | ||
|
|
55eb889aca | ||
|
|
e884f50d1c | ||
|
|
52e297e679 | ||
|
|
c3a3a44a37 | ||
|
|
19860865d1 | ||
|
|
6b023d8055 |
No files matched your search
@@ -1,7 +1,6 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- hhvm
|
||||
|
||||
+5
-5
@@ -12,11 +12,11 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/http": "~5.0",
|
||||
"illuminate/routing": "~5.0",
|
||||
"illuminate/session": "~5.0",
|
||||
"illuminate/support": "~5.0"
|
||||
"php": ">=5.5.9",
|
||||
"illuminate/http": "5.1.*",
|
||||
"illuminate/routing": "5.1.*",
|
||||
"illuminate/session": "5.1.*",
|
||||
"illuminate/support": "5.1.*"
|
||||
},
|
||||
"require-dev": {
|
||||
"mockery/mockery": "~0.9",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Forms & HTML
|
||||
|
||||
[](https://travis-ci.org/LaravelCollective/html)
|
||||
[](https://packagist.org/packages/laravelcollective/html)
|
||||
[](https://packagist.org/packages/laravelcollective/html)
|
||||
[](https://packagist.org/packages/laravelcollective/html)
|
||||
[](https://packagist.org/packages/laravelcollective/html)
|
||||
[](https://packagist.org/packages/laravelcollective/html)
|
||||
|
||||
+155
-50
@@ -4,6 +4,7 @@ use DateTime;
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Session\Store as Session;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FormBuilder {
|
||||
|
||||
@@ -49,28 +50,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 +79,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 +93,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');
|
||||
|
||||
@@ -138,9 +141,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 +155,7 @@ class FormBuilder {
|
||||
* Set the model instance on the form builder.
|
||||
*
|
||||
* @param mixed $model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setModel($model)
|
||||
@@ -165,7 +170,7 @@ class FormBuilder {
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->labels = array();
|
||||
$this->labels = [];
|
||||
|
||||
$this->model = null;
|
||||
|
||||
@@ -190,9 +195,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;
|
||||
|
||||
@@ -208,6 +214,7 @@ class FormBuilder {
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function formatLabel($name, $value)
|
||||
@@ -222,9 +229,10 @@ 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;
|
||||
|
||||
@@ -254,9 +262,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 +275,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 +289,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,22 +303,38 @@ 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a tel input field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function tel($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('tel', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a number input field.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
@@ -318,9 +345,10 @@ 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)
|
||||
{
|
||||
@@ -330,15 +358,30 @@ class FormBuilder {
|
||||
return $this->input('date', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a time input field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function time($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('time', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a url input field.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
@@ -348,9 +391,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);
|
||||
}
|
||||
@@ -361,9 +405,10 @@ 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;
|
||||
|
||||
@@ -390,6 +435,7 @@ class FormBuilder {
|
||||
* Set the text area size on the attributes.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function setTextAreaSize($options)
|
||||
@@ -413,13 +459,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]]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -429,9 +476,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
|
||||
@@ -445,7 +493,13 @@ class FormBuilder {
|
||||
// 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 = [];
|
||||
|
||||
if(isset($options['placeholder']))
|
||||
{
|
||||
$html[] = $this->placeholderOption($options['placeholder'], $selected);
|
||||
unset($options['placeholder']);
|
||||
}
|
||||
|
||||
foreach($list as $value => $display)
|
||||
{
|
||||
@@ -470,9 +524,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);
|
||||
|
||||
@@ -487,11 +542,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());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -501,11 +557,12 @@ 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)
|
||||
{
|
||||
@@ -521,6 +578,7 @@ class FormBuilder {
|
||||
* @param string $display
|
||||
* @param string $value
|
||||
* @param string $selected
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectOption($display, $value, $selected)
|
||||
@@ -539,11 +597,12 @@ 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)
|
||||
{
|
||||
@@ -559,13 +618,32 @@ 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' => $value, 'selected' => $selected];
|
||||
|
||||
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>';
|
||||
}
|
||||
@@ -575,7 +653,8 @@ class FormBuilder {
|
||||
*
|
||||
* @param string $value
|
||||
* @param string $selected
|
||||
* @return string
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getSelectedValue($value, $selected)
|
||||
{
|
||||
@@ -594,9 +673,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);
|
||||
}
|
||||
@@ -608,9 +688,10 @@ 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;
|
||||
|
||||
@@ -625,6 +706,7 @@ class FormBuilder {
|
||||
* @param mixed $value
|
||||
* @param bool $checked
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function checkable($type, $name, $value, $checked, $options)
|
||||
@@ -643,6 +725,7 @@ class FormBuilder {
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param bool $checked
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function getCheckedState($type, $name, $value, $checked)
|
||||
@@ -666,6 +749,7 @@ class FormBuilder {
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param bool $checked
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function getCheckboxCheckedState($name, $value, $checked)
|
||||
@@ -674,9 +758,20 @@ class FormBuilder {
|
||||
|
||||
if($this->missingOldAndModel($name)) return $checked;
|
||||
|
||||
$posted = $this->getValueAttribute($name);
|
||||
$posted = $this->getValueAttribute($name, $checked);
|
||||
|
||||
return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
|
||||
if (is_array($posted))
|
||||
{
|
||||
return in_array($value, $posted);
|
||||
}
|
||||
else if ($posted instanceof Collection)
|
||||
{
|
||||
return $posted->contains('id', $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bool) $posted;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -685,6 +780,7 @@ class FormBuilder {
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
* @param bool $checked
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function getRadioCheckedState($name, $value, $checked)
|
||||
@@ -698,6 +794,7 @@ class FormBuilder {
|
||||
* Determine if old input or model input exists for a key.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function missingOldAndModel($name)
|
||||
@@ -710,9 +807,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);
|
||||
}
|
||||
@@ -723,9 +821,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);
|
||||
|
||||
@@ -737,9 +836,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);
|
||||
}
|
||||
@@ -749,9 +849,10 @@ 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))
|
||||
{
|
||||
@@ -765,6 +866,7 @@ class FormBuilder {
|
||||
* Parse the form action method.
|
||||
*
|
||||
* @param string $method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getMethod($method)
|
||||
@@ -778,6 +880,7 @@ class FormBuilder {
|
||||
* Get the form action from the options.
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getAction(array $options)
|
||||
@@ -810,6 +913,7 @@ class FormBuilder {
|
||||
* Get the action for a "url" option.
|
||||
*
|
||||
* @param array|string $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getUrlAction($options)
|
||||
@@ -826,6 +930,7 @@ class FormBuilder {
|
||||
* Get the action for a "route" option.
|
||||
*
|
||||
* @param array|string $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getRouteAction($options)
|
||||
@@ -842,6 +947,7 @@ class FormBuilder {
|
||||
* Get the action for an "action" option.
|
||||
*
|
||||
* @param array|string $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getControllerAction($options)
|
||||
@@ -858,11 +964,12 @@ 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
|
||||
@@ -888,6 +995,7 @@ class FormBuilder {
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getIdAttribute($name, $attributes)
|
||||
@@ -908,7 +1016,8 @@ class FormBuilder {
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return string
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValueAttribute($name, $value = null)
|
||||
{
|
||||
@@ -931,25 +1040,20 @@ class FormBuilder {
|
||||
* Get the model value that should be assigned to the field.
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getModelValueAttribute($name)
|
||||
{
|
||||
if (is_object($this->model))
|
||||
{
|
||||
return object_get($this->model, $this->transformKey($name));
|
||||
}
|
||||
elseif (is_array($this->model))
|
||||
{
|
||||
return array_get($this->model, $this->transformKey($name));
|
||||
}
|
||||
return data_get($this->model, $this->transformKey($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the session's old input.
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function old($name)
|
||||
{
|
||||
@@ -973,11 +1077,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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -994,6 +1099,7 @@ class FormBuilder {
|
||||
* Set the session store implementation.
|
||||
*
|
||||
* @param \Illuminate\Session\Store $session
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSessionStore(Session $session)
|
||||
@@ -1002,5 +1108,4 @@ class FormBuilder {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
+24
-5
@@ -97,6 +97,25 @@ class HtmlBuilder {
|
||||
return '<img src="'.$this->url->asset($url, $secure).'"'.$this->attributes($attributes).'>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a link to a Favicon file.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $attributes
|
||||
* @param bool $secure
|
||||
* @return string
|
||||
*/
|
||||
public function favicon($url, $attributes = array(), $secure = null)
|
||||
{
|
||||
$defaults = array('rel' => 'shortcut icon', 'type' => 'image/x-icon');
|
||||
|
||||
$attributes = $attributes + $defaults;
|
||||
|
||||
$attributes['href'] = $this->url->asset($url, $secure);
|
||||
|
||||
return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a HTML link.
|
||||
*
|
||||
@@ -294,7 +313,7 @@ class HtmlBuilder {
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param string $type
|
||||
* @param string $value
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
protected function listingElement($key, $type, $value)
|
||||
@@ -314,7 +333,7 @@ class HtmlBuilder {
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param string $type
|
||||
* @param string $value
|
||||
* @param mixed $value
|
||||
* @return string
|
||||
*/
|
||||
protected function nestedListing($key, $type, $value)
|
||||
@@ -339,9 +358,6 @@ class HtmlBuilder {
|
||||
{
|
||||
$html = array();
|
||||
|
||||
// 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.
|
||||
foreach ((array) $attributes as $key => $value)
|
||||
{
|
||||
$element = $this->attributeElement($key, $value);
|
||||
@@ -361,6 +377,9 @@ class HtmlBuilder {
|
||||
*/
|
||||
protected function attributeElement($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;
|
||||
|
||||
if ( ! is_null($value)) return $key.'="'.e($value).'"';
|
||||
|
||||
@@ -61,7 +61,7 @@ class HtmlServiceProvider extends ServiceProvider {
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return array('html', 'form');
|
||||
return array('html', 'form', 'Collective\Html\HtmlBuilder', 'Collective\Html\FormBuilder');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use Collective\Html\FormBuilder;
|
||||
use Collective\Html\HtmlBuilder;
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Routing\RouteCollection;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FormBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
@@ -137,6 +138,18 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
|
||||
public function testFormRepopulationWithMixOfArraysAndObjects()
|
||||
{
|
||||
$this->formBuilder->model(['user' => (object)['password' => 'apple']]);
|
||||
$input = $this->formBuilder->text('user[password]');
|
||||
$this->assertEquals('<input name="user[password]" type="text" value="apple">', $input);
|
||||
|
||||
$this->formBuilder->model((object)['letters' => ['a', 'b', 'c']]);
|
||||
$input = $this->formBuilder->text('letters[1]');
|
||||
$this->assertEquals('<input name="letters[1]" type="text" value="b">', $input);
|
||||
}
|
||||
|
||||
|
||||
public function testFormPassword()
|
||||
{
|
||||
$form1 = $this->formBuilder->password('foo');
|
||||
@@ -171,6 +184,18 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
|
||||
public function testFormTel()
|
||||
{
|
||||
$form1 = $this->formBuilder->tel('foo');
|
||||
$form2 = $this->formBuilder->tel('foo', 'foobar');
|
||||
$form3 = $this->formBuilder->tel('foo', null, ['class' => 'span2']);
|
||||
|
||||
$this->assertEquals('<input name="foo" type="tel">', $form1);
|
||||
$this->assertEquals('<input name="foo" type="tel" value="foobar">', $form2);
|
||||
$this->assertEquals('<input class="span2" name="foo" type="tel">', $form3);
|
||||
}
|
||||
|
||||
|
||||
public function testFormNumber()
|
||||
{
|
||||
$form1 = $this->formBuilder->number('foo');
|
||||
@@ -195,6 +220,17 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('<input class="span2" name="foo" type="date">', $form4);
|
||||
}
|
||||
|
||||
public function testFormTime()
|
||||
{
|
||||
$form1 = $this->formBuilder->time('foo');
|
||||
$form2 = $this->formBuilder->time('foo', \Carbon\Carbon::now()->format('H:i'));
|
||||
$form3 = $this->formBuilder->time('foo', null, ['class' => 'span2']);
|
||||
|
||||
$this->assertEquals('<input name="foo" type="time">', $form1);
|
||||
$this->assertEquals('<input name="foo" type="time" value="' . \Carbon\Carbon::now()->format('H:i') . '">', $form2);
|
||||
$this->assertEquals('<input class="span2" name="foo" type="time">', $form3);
|
||||
}
|
||||
|
||||
|
||||
public function testFormFile()
|
||||
{
|
||||
@@ -279,6 +315,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()
|
||||
{
|
||||
@@ -356,6 +410,30 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
|
||||
public function testFormCheckboxWithModelRelation()
|
||||
{
|
||||
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Session\Store'));
|
||||
$session->shouldReceive('getOldInput')->withNoArgs()->andReturn(array());
|
||||
$session->shouldReceive('getOldInput')->with('items')->andReturn(null);
|
||||
|
||||
$mockModel2 = new StdClass;
|
||||
$mockModel2->id = 2;
|
||||
$mockModel3 = new StdClass;
|
||||
$mockModel3->id = 3;
|
||||
$this->setModel(array('items' => new Collection(array($mockModel2, $mockModel3))));
|
||||
|
||||
$check1 = $this->formBuilder->checkbox('items[]', 1);
|
||||
$check2 = $this->formBuilder->checkbox('items[]', 2);
|
||||
$check3 = $this->formBuilder->checkbox('items[]', 3, false);
|
||||
$check4 = $this->formBuilder->checkbox('items[]', 4, true);
|
||||
|
||||
$this->assertEquals('<input name="items[]" type="checkbox" value="1">', $check1);
|
||||
$this->assertEquals('<input checked="checked" name="items[]" type="checkbox" value="2">', $check2);
|
||||
$this->assertEquals('<input name="items[]" type="checkbox" value="3">', $check3);
|
||||
$this->assertEquals('<input checked="checked" name="items[]" type="checkbox" value="4">', $check4);
|
||||
}
|
||||
|
||||
|
||||
public function testFormCheckboxWithoutSession()
|
||||
{
|
||||
$form1 = $this->formBuilder->checkbox('foo');
|
||||
|
||||
@@ -55,4 +55,13 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('<meta content="website" property="og:type">'.PHP_EOL, $result);
|
||||
}
|
||||
|
||||
public function testFavicon()
|
||||
{
|
||||
$this->urlGenerator->forceRootUrl('http://foo.com');
|
||||
$target = $this->urlGenerator->to('bar.ico');
|
||||
$result = $this->htmlBuilder->favicon('http://foo.com/bar.ico');
|
||||
|
||||
$this->assertEquals('<link rel="shortcut icon" type="image/x-icon" href="'.$target.'">' . PHP_EOL, $result);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user