Upgrade framework
This commit is contained in:
242
vendor/laravelcollective/html/src/FormBuilder.php
vendored
242
vendor/laravelcollective/html/src/FormBuilder.php
vendored
@@ -9,8 +9,8 @@ use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
|
||||
@@ -49,6 +49,12 @@ class FormBuilder
|
||||
*/
|
||||
protected $csrfToken;
|
||||
|
||||
/**
|
||||
* Consider Request variables while auto fill.
|
||||
* @var bool
|
||||
*/
|
||||
protected $considerRequest = false;
|
||||
|
||||
/**
|
||||
* The session store implementation.
|
||||
*
|
||||
@@ -108,6 +114,7 @@ class FormBuilder
|
||||
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
|
||||
* @param \Illuminate\Contracts\View\Factory $view
|
||||
* @param string $csrfToken
|
||||
* @param Request $request
|
||||
*/
|
||||
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null)
|
||||
{
|
||||
@@ -127,7 +134,7 @@ class FormBuilder
|
||||
*/
|
||||
public function open(array $options = [])
|
||||
{
|
||||
$method = array_get($options, 'method', 'post');
|
||||
$method = Arr::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
|
||||
@@ -152,7 +159,7 @@ class FormBuilder
|
||||
// is used to spoof requests for this PUT, PATCH, etc. methods on forms.
|
||||
$attributes = array_merge(
|
||||
|
||||
$attributes, array_except($options, $this->reserved)
|
||||
$attributes, Arr::except($options, $this->reserved)
|
||||
|
||||
);
|
||||
|
||||
@@ -191,6 +198,16 @@ class FormBuilder
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current model instance on the form builder.
|
||||
*
|
||||
* @return mixed $model
|
||||
*/
|
||||
public function getModel()
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the current form.
|
||||
*
|
||||
@@ -319,6 +336,20 @@ class FormBuilder
|
||||
return $this->input('password', $name, '', $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a range input field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param array $options
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function range($name, $value = null, $options = [])
|
||||
{
|
||||
return $this->input('range', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a hidden input field.
|
||||
*
|
||||
@@ -454,6 +485,10 @@ class FormBuilder
|
||||
*/
|
||||
public function time($name, $value = null, $options = [])
|
||||
{
|
||||
if ($value instanceof DateTime) {
|
||||
$value = $value->format('H:i');
|
||||
}
|
||||
|
||||
return $this->input('time', $name, $value, $options);
|
||||
}
|
||||
|
||||
@@ -471,6 +506,24 @@ class FormBuilder
|
||||
return $this->input('url', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a week input field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param array $options
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function week($name, $value = null, $options = [])
|
||||
{
|
||||
if ($value instanceof DateTime) {
|
||||
$value = $value->format('Y-\WW');
|
||||
}
|
||||
|
||||
return $this->input('week', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a file input field.
|
||||
*
|
||||
@@ -517,7 +570,7 @@ class FormBuilder
|
||||
// the element. Then we'll create the final textarea elements HTML for us.
|
||||
$options = $this->html->attributes($options);
|
||||
|
||||
return $this->toHtmlString('<textarea' . $options . '>' . e($value). '</textarea>');
|
||||
return $this->toHtmlString('<textarea' . $options . '>' . e($value, false). '</textarea>');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -536,9 +589,9 @@ class FormBuilder
|
||||
// If the "size" attribute was not specified, we will just look for the regular
|
||||
// columns and rows attributes, using sane defaults if these do not exist on
|
||||
// the attributes array. We'll then return this entire options array back.
|
||||
$cols = array_get($options, 'cols', 50);
|
||||
$cols = Arr::get($options, 'cols', 50);
|
||||
|
||||
$rows = array_get($options, 'rows', 10);
|
||||
$rows = Arr::get($options, 'rows', 10);
|
||||
|
||||
return array_merge($options, compact('cols', 'rows'));
|
||||
}
|
||||
@@ -562,9 +615,10 @@ class FormBuilder
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $list
|
||||
* @param string $selected
|
||||
* @param string|bool $selected
|
||||
* @param array $selectAttributes
|
||||
* @param array $optionsAttributes
|
||||
* @param array $optgroupsAttributes
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
@@ -573,7 +627,8 @@ class FormBuilder
|
||||
$list = [],
|
||||
$selected = null,
|
||||
array $selectAttributes = [],
|
||||
array $optionsAttributes = []
|
||||
array $optionsAttributes = [],
|
||||
array $optgroupsAttributes = []
|
||||
) {
|
||||
$this->type = 'select';
|
||||
|
||||
@@ -599,8 +654,9 @@ class FormBuilder
|
||||
}
|
||||
|
||||
foreach ($list as $value => $display) {
|
||||
$optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : [];
|
||||
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes);
|
||||
$optionAttributes = $optionsAttributes[$value] ?? [];
|
||||
$optgroupAttributes = $optgroupsAttributes[$value] ?? [];
|
||||
$html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
|
||||
}
|
||||
|
||||
// Once we have all of this HTML, we can join this into a single element after
|
||||
@@ -675,13 +731,14 @@ class FormBuilder
|
||||
* @param string $value
|
||||
* @param string $selected
|
||||
* @param array $attributes
|
||||
* @param array $optgroupAttributes
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function getSelectOption($display, $value, $selected, array $attributes = [])
|
||||
public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
|
||||
{
|
||||
if (is_array($display)) {
|
||||
return $this->optionGroup($display, $value, $selected, $attributes);
|
||||
if (is_iterable($display)) {
|
||||
return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
|
||||
}
|
||||
|
||||
return $this->option($display, $value, $selected, $attributes);
|
||||
@@ -694,18 +751,24 @@ class FormBuilder
|
||||
* @param string $label
|
||||
* @param string $selected
|
||||
* @param array $attributes
|
||||
* @param array $optionsAttributes
|
||||
* @param integer $level
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
protected function optionGroup($list, $label, $selected, array $attributes = [])
|
||||
protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0)
|
||||
{
|
||||
$html = [];
|
||||
|
||||
$space = str_repeat(" ", $level);
|
||||
foreach ($list as $value => $display) {
|
||||
$html[] = $this->option($display, $value, $selected, $attributes);
|
||||
$optionAttributes = $optionsAttributes[$value] ?? [];
|
||||
if (is_iterable($display)) {
|
||||
$html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5);
|
||||
} else {
|
||||
$html[] = $this->option($space.$display, $value, $selected, $optionAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->toHtmlString('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');
|
||||
return $this->toHtmlString('<optgroup label="' . e($space.$label, false) . '"' . $this->html->attributes($attributes) . '>' . implode('', $html) . '</optgroup>');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -722,9 +785,14 @@ class FormBuilder
|
||||
{
|
||||
$selected = $this->getSelectedValue($value, $selected);
|
||||
|
||||
$options = ['value' => $value, 'selected' => $selected] + $attributes;
|
||||
$options = array_merge(['value' => $value, 'selected' => $selected], $attributes);
|
||||
|
||||
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
|
||||
$string = '<option' . $this->html->attributes($options) . '>';
|
||||
if ($display !== null) {
|
||||
$string .= e($display, false) . '</option>';
|
||||
}
|
||||
|
||||
return $this->toHtmlString($string);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -741,12 +809,10 @@ class FormBuilder
|
||||
|
||||
$options = [
|
||||
'selected' => $selected,
|
||||
'disabled' => 'disabled',
|
||||
'hidden' => 'hidden',
|
||||
'value' => ''
|
||||
'value' => '',
|
||||
];
|
||||
|
||||
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
|
||||
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display, false) . '</option>');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -760,12 +826,14 @@ class FormBuilder
|
||||
protected function getSelectedValue($value, $selected)
|
||||
{
|
||||
if (is_array($selected)) {
|
||||
return in_array($value, $selected, true) ? 'selected' : null;
|
||||
return in_array($value, $selected, true) || in_array((string) $value, $selected, true) ? 'selected' : null;
|
||||
} elseif ($selected instanceof Collection) {
|
||||
return $selected->contains($value) ? 'selected' : null;
|
||||
}
|
||||
|
||||
return ((string) $value == (string) $selected) ? 'selected' : null;
|
||||
if (is_int($value) && is_bool($selected)) {
|
||||
return (bool)$value === $selected;
|
||||
}
|
||||
return ((string) $value === (string) $selected) ? 'selected' : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -846,7 +914,7 @@ class FormBuilder
|
||||
return $this->getRadioCheckedState($name, $value, $checked);
|
||||
|
||||
default:
|
||||
return $this->getValueAttribute($name) == $value;
|
||||
return $this->compareValues($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -867,7 +935,7 @@ class FormBuilder
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->missingOldAndModel($name) && !$request) {
|
||||
if ($this->missingOldAndModel($name) && is_null($request)) {
|
||||
return $checked;
|
||||
}
|
||||
|
||||
@@ -899,6 +967,20 @@ class FormBuilder
|
||||
return $checked;
|
||||
}
|
||||
|
||||
return $this->compareValues($name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the provide value loosely compares to the value assigned to the field.
|
||||
* Use loose comparison because Laravel model casting may be in affect and therefore
|
||||
* 1 == true and 0 == false.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function compareValues($name, $value)
|
||||
{
|
||||
return $this->getValueAttribute($name) == $value;
|
||||
}
|
||||
|
||||
@@ -943,6 +1025,24 @@ class FormBuilder
|
||||
return $this->input('image', $name, null, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a month input field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param array $options
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function month($name, $value = null, $options = [])
|
||||
{
|
||||
if ($value instanceof DateTime) {
|
||||
$value = $value->format('Y-m');
|
||||
}
|
||||
|
||||
return $this->input('month', $name, $value, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a color input field.
|
||||
*
|
||||
@@ -987,6 +1087,50 @@ class FormBuilder
|
||||
return $this->toHtmlString('<button' . $this->html->attributes($options) . '>' . $value . '</button>');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a datalist box field.
|
||||
*
|
||||
* @param string $id
|
||||
* @param array $list
|
||||
*
|
||||
* @return \Illuminate\Support\HtmlString
|
||||
*/
|
||||
public function datalist($id, $list = [])
|
||||
{
|
||||
$this->type = 'datalist';
|
||||
|
||||
$attributes['id'] = $id;
|
||||
|
||||
$html = [];
|
||||
|
||||
if ($this->isAssociativeArray($list)) {
|
||||
foreach ($list as $value => $display) {
|
||||
$html[] = $this->option($display, $value, null, []);
|
||||
}
|
||||
} else {
|
||||
foreach ($list as $value) {
|
||||
$html[] = $this->option($value, $value, null, []);
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = $this->html->attributes($attributes);
|
||||
|
||||
$list = implode('', $html);
|
||||
|
||||
return $this->toHtmlString("<datalist{$attributes}>{$list}</datalist>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an array is associative.
|
||||
*
|
||||
* @param array $array
|
||||
* @return bool
|
||||
*/
|
||||
protected function isAssociativeArray($array)
|
||||
{
|
||||
return (array_values($array) !== $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the form action method.
|
||||
*
|
||||
@@ -998,7 +1142,7 @@ class FormBuilder
|
||||
{
|
||||
$method = strtoupper($method);
|
||||
|
||||
return $method != 'GET' ? 'POST' : $method;
|
||||
return $method !== 'GET' ? 'POST' : $method;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1057,7 +1201,13 @@ class FormBuilder
|
||||
protected function getRouteAction($options)
|
||||
{
|
||||
if (is_array($options)) {
|
||||
return $this->url->route($options[0], array_slice($options, 1));
|
||||
$parameters = array_slice($options, 1);
|
||||
|
||||
if (array_keys($options) === [0, 1]) {
|
||||
$parameters = head($parameters);
|
||||
}
|
||||
|
||||
return $this->url->route($options[0], $parameters);
|
||||
}
|
||||
|
||||
return $this->url->route($options);
|
||||
@@ -1100,7 +1250,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();
|
||||
}
|
||||
|
||||
@@ -1142,7 +1292,7 @@ class FormBuilder
|
||||
|
||||
$old = $this->old($name);
|
||||
|
||||
if (! is_null($old) && $name != '_method') {
|
||||
if (! is_null($old) && $name !== '_method') {
|
||||
return $old;
|
||||
}
|
||||
|
||||
@@ -1154,14 +1304,14 @@ class FormBuilder
|
||||
&& is_null($old)
|
||||
&& is_null($value)
|
||||
&& !is_null($this->view->shared('errors'))
|
||||
&& count($this->view->shared('errors')) > 0
|
||||
&& count(is_countable($this->view->shared('errors')) ? $this->view->shared('errors') : []) > 0
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$request = $this->request($name);
|
||||
if (!is_null($request)) {
|
||||
if (! is_null($request) && $name != '_method') {
|
||||
return $request;
|
||||
}
|
||||
|
||||
@@ -1174,6 +1324,15 @@ class FormBuilder
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Take Request in fill process
|
||||
* @param bool $consider
|
||||
*/
|
||||
public function considerRequest($consider = true)
|
||||
{
|
||||
$this->considerRequest = $consider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get value from current Request
|
||||
* @param $name
|
||||
@@ -1181,6 +1340,10 @@ class FormBuilder
|
||||
*/
|
||||
protected function request($name)
|
||||
{
|
||||
if (!$this->considerRequest) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isset($this->request)) {
|
||||
return null;
|
||||
}
|
||||
@@ -1192,19 +1355,18 @@ class FormBuilder
|
||||
* Get the model value that should be assigned to the field.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $model
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getModelValueAttribute($name, $model = null)
|
||||
protected function getModelValueAttribute($name)
|
||||
{
|
||||
$key = $this->transformKey($name);
|
||||
|
||||
if (method_exists($this->model, 'getFormValue')) {
|
||||
if ((is_string($this->model) || is_object($this->model)) && method_exists($this->model, 'getFormValue')) {
|
||||
return $this->model->getFormValue($key);
|
||||
}
|
||||
|
||||
return data_get($this->model, $this->transformKey($name));
|
||||
return data_get($this->model, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1246,7 +1408,7 @@ class FormBuilder
|
||||
*/
|
||||
public function oldInputIsEmpty()
|
||||
{
|
||||
return (isset($this->session) && count($this->session->getOldInput()) == 0);
|
||||
return (isset($this->session) && count((array) $this->session->getOldInput()) === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user