Skip to content

Commit

Permalink
Merge pull request #23 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Updating the Form::select() method
  • Loading branch information
arcanedev-maroc committed Mar 10, 2018
2 parents 0a3c250 + 6e2224c commit 4ed9d0e
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 82 deletions.
14 changes: 11 additions & 3 deletions src/Contracts/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,21 @@ public function textarea($name, $value = null, array $options = []);
*
* @param string $name
* @param array|\Illuminate\Support\Collection $list
* @param string $selected
* @param string|bool $selected
* @param array $attributes
* @param array $options
* @param array $optionsAttributes
* @param array $optgroupsAttributes
*
* @return \Illuminate\Support\HtmlString
*/
public function select($name, $list = [], $selected = null, array $attributes = [], array $options = []);
public function select(
$name,
$list = [],
$selected = null,
array $attributes = [],
array $optionsAttributes = [],
array $optgroupsAttributes = []
);

/**
* Create a select range field.
Expand Down
146 changes: 74 additions & 72 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FormBuilder extends Builder implements FormBuilderContract
/**
* The session store implementation.
*
* @var \Illuminate\Contracts\Session\Session
* @var \Illuminate\Contracts\Session\Session|\Illuminate\Session\Store
*/
protected $session;

Expand Down Expand Up @@ -214,9 +214,8 @@ public function getValueAttribute($name, $value = null)
*/
private function getModelValueAttribute($name, $model = null)
{
if (is_null($model)) {
if (is_null($model))
$model = $this->model;
}

$key = $this->transformKey($name);

Expand Down Expand Up @@ -308,9 +307,8 @@ public function open(array $options = [])
'accept-charset' => 'UTF-8',
];

if (isset($options['files']) && $options['files']) {
if (isset($options['files']) && $options['files'])
$options['enctype'] = 'multipart/form-data';
}

// 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
Expand Down Expand Up @@ -367,11 +365,10 @@ public function close()
*/
public function token()
{
$token = ! empty($this->csrfToken)
? $this->csrfToken
: $this->session->token();

return $this->hidden('_token', $token);
return $this->hidden(
'_token',
empty($this->csrfToken) ? $this->session->token() : $this->csrfToken
);
}

/**
Expand Down Expand Up @@ -405,18 +402,16 @@ public function label($name, $value = null, array $options = [], $escaped = true
*/
public function input($type, $name, $value = null, array $options = [])
{
if ( ! isset($options['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);
}

// Once we have the type, value, and ID we can merge them into the rest of the
// attributes array so we can convert them into their HTML attribute format
Expand Down Expand Up @@ -520,9 +515,8 @@ public function number($name, $value = null, array $options = [])
*/
public function date($name, $value = null, array $options = [])
{
if ($value instanceof DateTime) {
if ($value instanceof DateTime)
$value = $value->format('Y-m-d');
}

return $this->input('date', $name, $value, $options);
}
Expand All @@ -538,9 +532,8 @@ public function date($name, $value = null, array $options = [])
*/
public function datetime($name, $value = null, array $options = [])
{
if ($value instanceof DateTime) {
if ($value instanceof DateTime)
$value = $value->format(DateTime::RFC3339);
}

return $this->input('datetime', $name, $value, $options);
}
Expand All @@ -556,9 +549,8 @@ public function datetime($name, $value = null, array $options = [])
*/
public function datetimeLocal($name, $value = null, array $options = [])
{
if ($value instanceof DateTime) {
if ($value instanceof DateTime)
$value = $value->format('Y-m-d\TH:i');
}

return $this->input('datetime-local', $name, $value, $options);
}
Expand Down Expand Up @@ -615,9 +607,8 @@ public function file($name, array $options = [])
*/
public function textarea($name, $value = null, array $options = [])
{
if ( ! isset($options['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
Expand Down Expand Up @@ -645,9 +636,8 @@ public function textarea($name, $value = null, array $options = [])
*/
private function setTextAreaSize(array $options)
{
if (isset($options['size'])) {
if (isset($options['size']))
return $this->setQuickTextAreaSize($options);
}

// 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
Expand Down Expand Up @@ -677,29 +667,34 @@ protected function setQuickTextAreaSize(array $options)
*
* @param string $name
* @param array|\Illuminate\Support\Collection $list
* @param string $selected
* @param string|bool $selected
* @param array $attributes
* @param array $options
* @param array $optionsAttributes
* @param array $optgroupsAttributes
*
* @return \Illuminate\Support\HtmlString
*/
public function select($name, $list = [], $selected = null, array $attributes = [], array $options = [])
{
public function select(
$name,
$list = [],
$selected = null,
array $attributes = [],
array $optionsAttributes = [],
array $optgroupsAttributes = []
) {
// 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
// should provide a convenient method of re-populating the forms on post.
$selected = $this->getValueAttribute($name, $selected);

// Transform to array if it is a collection
if ($selected instanceof Collection) {
if ($selected instanceof Collection)
$selected = $selected->all();
}

$attributes['id'] = $this->getIdAttribute($name, $attributes);

if ( ! isset($attributes['name'])) {
if ( ! isset($attributes['name']))
$attributes['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
Expand All @@ -712,9 +707,10 @@ public function select($name, $list = [], $selected = null, array $attributes =
}

foreach($list as $value => $display) {
$html[] = $this->getSelectOption(
$display, $value, $selected, isset($options[$value]) ? $options[$value] : []
);
$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
Expand Down Expand Up @@ -790,13 +786,14 @@ public function selectMonth($name, $selected = null, array $options = [], $forma
* @param string $value
* @param string $selected
* @param array $attributes
* @param array $optgroupAttributes
*
* @return string
*/
private function getSelectOption($display, $value, $selected, array $attributes = [])
private function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
{
return is_array($display)
? $this->optionGroup($display, $value, $selected, $attributes)
? $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes)
: $this->option($display, $value, $selected, $attributes);
}

Expand All @@ -807,18 +804,25 @@ private function getSelectOption($display, $value, $selected, array $attributes
* @param string $label
* @param string $selected
* @param array $attributes
* @param array $optionsAttributes
* @param int $level
*
* @return string
*/
private function optionGroup(array $list, $label, $selected, array $attributes = [])
private function optionGroup(array $list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0)
{
$html = [];
$html = [];
$space = str_repeat(" ", $level);

foreach($list as $value => $display) {
$html[] = $this->option($display, $value, $selected, $attributes);
$optionAttributes = $optionsAttributes[$value] ?? [];

$html[] = is_array($display)
? $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level + 5)
: $this->option($space.$display, $value, $selected, $optionAttributes);
}

return '<optgroup label="'.$this->html->escape($label).'">'.implode('', $html).'</optgroup>';
return '<optgroup label="'.$this->html->escape($label).'"'.$this->html->attributes($attributes).'>'.implode('', $html).'</optgroup>';
}

/**
Expand Down Expand Up @@ -865,9 +869,14 @@ private function placeholderOption($display, $selected)
*/
private function getSelectedValue($value, $selected)
{
if (is_array($selected)) {
return in_array($value, $selected) ? 'selected' : null;
}
if (is_array($selected))
return (in_array($value, $selected, true) || in_array((string) $value, $selected, true)) ? 'selected' : null;

if ($selected instanceof Collection)
return $selected->contains($value) ? 'selected' : null;

if (is_int($value) && is_bool($selected))
return (bool) $value === $selected;

return ((string) $value === (string) $selected) ? 'selected' : null;
}
Expand Down Expand Up @@ -899,9 +908,7 @@ public function checkbox($name, $value = 1, $checked = null, array $options = []
*/
public function radio($name, $value = null, $checked = null, array $options = [])
{
$value = $value ?? $name;

return $this->checkable('radio', $name, $value, $checked, $options);
return $this->checkable('radio', $name, $value ?? $name, $checked, $options);
}

/**
Expand All @@ -919,9 +926,8 @@ protected function checkable($type, $name, $value, $checked, array $options)
{
$checked = $this->getCheckedState($type, $name, $value, $checked);

if ( ! is_null($checked) && $checked) {
if ( ! is_null($checked) && $checked)
$options['checked'] = 'checked';
}

return $this->input($type, $name, $value, $options);
}
Expand Down Expand Up @@ -961,23 +967,23 @@ private function getCheckedState($type, $name, $value, $checked)
*/
private function getCheckboxCheckedState($name, $value, $checked)
{
if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) {
if (
isset($this->session) &&
! $this->oldInputIsEmpty() &&
is_null($this->old($name))
)
return false;
}

if ($this->missingOldAndModel($name)) {
if ($this->missingOldAndModel($name))
return $checked;
}

$posted = $this->getValueAttribute($name, $checked);

if (is_array($posted)) {
if (is_array($posted))
return in_array($value, $posted);
}

if ($posted instanceof Collection) {
if ($posted instanceof Collection)
return $posted->contains('id', $value);
}

return (bool) $posted;
}
Expand Down Expand Up @@ -1034,9 +1040,9 @@ public function reset($value, array $attributes = [])
*/
public function image($url, $name = null, array $attributes = [])
{
$attributes['src'] = $this->url->asset($url);

return $this->input('image', $name, null, $attributes);
return $this->input('image', $name, null, array_merge($attributes, [
'src' => $this->url->asset($url),
]));
}

/**
Expand All @@ -1062,9 +1068,8 @@ public function submit($value = null, array $options = [])
*/
public function button($value = null, array $options = [])
{
if ( ! array_key_exists('type', $options)) {
if ( ! array_key_exists('type', $options))
$options['type'] = 'button';
}

return $this->toHtmlString(
'<button'.$this->html->attributes($options).'>'.$value.'</button>'
Expand Down Expand Up @@ -1102,18 +1107,17 @@ private function getAction(array $options)
// 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']);
}
elseif (isset($options['route'])) {

if (isset($options['route']))
return $this->getRouteAction($options['route']);
}
elseif (isset($options['action'])) {

if (isset($options['action']))
// 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.
return $this->getControllerAction($options['action']);
}

return $this->url->current();
}
Expand Down Expand Up @@ -1174,16 +1178,14 @@ private function getAppendage($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);
}

// 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();
}

return $appendage;
}
Expand Down
Loading

0 comments on commit 4ed9d0e

Please sign in to comment.