Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple select #101

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "netojose/laravel-bootstrap-4-forms",
"name": "zitsky/laravel-bootstrap-4-forms",
"type": "package",
"description": "Bootstrap 4 form builder for Laravel 5",
"keywords": [
Expand Down
68 changes: 36 additions & 32 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ public function set($key, $value)
$this->attrs[$key] = $value;
}

private function formatMethod($value)
protected function formatMethod($value)
{
return strtolower($value);
}

private function formatFormData($value)
protected function formatFormData($value)
{
if (is_object($value) && method_exists($value, 'toArray')) {
return $value->toArray();
}
return $value;
}

private function formatOptions($value)
protected function formatOptions($value)
{
extract($this->get('optionIdKey', 'optionValueKey'));

Expand All @@ -58,7 +58,7 @@ public function render(): string
return $output;
}

private function renderFormOpen(): string
protected function renderFormOpen(): string
{
extract($this->get('id', 'method', 'url', 'formMultipart', 'formInline', 'autocomplete'));

Expand Down Expand Up @@ -89,13 +89,13 @@ private function renderFormOpen(): string
return $output;
}

private function renderFormClose(): string
protected function renderFormClose(): string
{
$this->resetAttributes(true);
return '</form>';
}

private function renderFieldsetOpen(): string
protected function renderFieldsetOpen(): string
{
$output = '<fieldset>';
extract($this->get('legend'));
Expand All @@ -107,12 +107,12 @@ private function renderFieldsetOpen(): string
return $output;
}

private function renderFieldsetClose(): string
protected function renderFieldsetClose(): string
{
return '</fieldset>';
}

private function renderErrors(): string
protected function renderErrors(): string
{
$errors = $this->errors()->all();
if (count($errors) < 1) {
Expand All @@ -131,14 +131,14 @@ private function renderErrors(): string
return $output . '</ul></div>';
}

private function renderInput(): string
protected function renderInput(): string
{
$attributes = $this->getInputAttributes();
$attrs = $this->buildHtmlAttrs($attributes);
return $this->wrapperInput('<input ' . $attrs . '>');
}

private function renderSelect(): string
protected function renderSelect(): string
{
extract($this->get('options'));

Expand All @@ -151,11 +151,14 @@ private function renderSelect(): string
}

$attributes = $this->getInputAttributes();
if(isset($attributes['multiple'])) {
$attributes['name'] .= "[]";
}
$attrs = $this->buildHtmlAttrs($attributes);
return $this->wrapperInput('<select ' . $attrs . '>' . $optionsList . '</select>');
}

private function renderTextarea(): string
protected function renderTextarea(): string
{
$attributes = $this->getInputAttributes();
$value = $attributes['value'];
Expand All @@ -164,37 +167,37 @@ private function renderTextarea(): string
return $this->wrapperInput('<textarea ' . $attrs . '>' . htmlspecialchars($value) . '</textarea>');
}

private function renderCheckbox(): string
protected function renderCheckbox(): string
{
$attributes = $this->getInputAttributes();
$attrs = $this->buildHtmlAttrs($attributes);
return $this->wrapperRadioCheckbox('<input ' . $attrs . '>');
}

private function renderRadio(): string
protected function renderRadio(): string
{
$attributes = $this->getInputAttributes();
$attrs = $this->buildHtmlAttrs($attributes);
return $this->wrapperRadioCheckbox('<input ' . $attrs . '>');
}

private function renderAnchor(): string
protected function renderAnchor(): string
{
extract($this->get('url', 'value'));
$class = $this->getBtnAnchorClasses();
$attrs = $this->buildHtmlAttrs(['href' => $url, 'class' => $class]);
return '<a ' . $attrs . '>' . $value . '</a>';
}

private function renderButton(): string
protected function renderButton(): string
{
extract($this->get('type', 'value', 'disabled'));
$class = $this->getBtnAnchorClasses();
$attrs = $this->buildHtmlAttrs(['type' => $type, 'class' => $class, 'disabled' => $disabled]);
return '<button ' . $attrs . '>' . $value . '</button>';
}

private function getBtnAnchorClasses()
protected function getBtnAnchorClasses()
{
extract($this->get('size', 'color', 'outline', 'block', 'type', 'value', 'formInline'));
return $this->createAttrsList(
Expand All @@ -206,13 +209,13 @@ private function getBtnAnchorClasses()
);
}

private function isRadioOrCheckbox(): bool
protected function isRadioOrCheckbox(): bool
{
extract($this->get('render'));
return in_array($render, ['checkbox', 'radio']);
}

private function getInputAttributes(): array
protected function getInputAttributes(): array
{
extract($this->get('render', 'type', 'multiple', 'name', 'size', 'placeholder', 'help', 'disabled', 'readonly', 'required', 'autocomplete', 'min', 'max', 'value', 'checked', 'formData', 'disableValidation'));

Expand Down Expand Up @@ -263,6 +266,7 @@ private function getInputAttributes(): array
if ($this->hasOldInput()) {
$isChecked = old($name) === $value;
} else {
$value = $value === 'on' ? true : false;
$isChecked = isset($formData[$name]) ? $formData[$name] === $value : $checked;
}
$attributes['checked'] = $isChecked;
Expand All @@ -281,7 +285,7 @@ private function getInputAttributes(): array
]);
}

private function renderLabel(): string
protected function renderLabel(): string
{
extract($this->get('label', 'formInline', 'render'));

Expand All @@ -298,7 +302,7 @@ private function renderLabel(): string
return '<label ' . $attrs . '>' . $this->getText($label) . '</label>';
}

private function getText($key)
protected function getText($key)
{
extract($this->get('formLocale'));
if ($formLocale) {
Expand All @@ -307,7 +311,7 @@ private function getText($key)
return $key;
}

private function resetAttributes($resetAll = false)
protected function resetAttributes($resetAll = false)
{
// Remove all attributes
if ($resetAll) {
Expand All @@ -321,7 +325,7 @@ private function resetAttributes($resetAll = false)
}, ARRAY_FILTER_USE_KEY);
}

private function wrapperInput(string $input): string
protected function wrapperInput(string $input): string
{
extract($this->get('type', 'help', 'wrapperAttrs', 'formInline', 'name'));

Expand All @@ -343,7 +347,7 @@ private function wrapperInput(string $input): string
return '<div ' . $attributes . '>' . $label . $input . $helpText . $error . '</div>';
}

private function wrapperRadioCheckbox(string $input): string
protected function wrapperRadioCheckbox(string $input): string
{
extract($this->get('inline', 'name', 'wrapperAttrs'));

Expand All @@ -359,7 +363,7 @@ private function wrapperRadioCheckbox(string $input): string
return '<div ' . $attributes . '>' . $input . $label . $error . '</div>';
}

private function getInputErrorMarkup(string $name): string
protected function getInputErrorMarkup(string $name): string
{
extract($this->get('disableValidation'));

Expand All @@ -374,7 +378,7 @@ private function getInputErrorMarkup(string $name): string
return '<div class="invalid-feedback">' . $this->errors()->first($name) . '</div>';
}

private function getId()
protected function getId()
{
extract($this->get('id', 'name', 'formIdPrefix', 'render', 'value'));

Expand All @@ -385,28 +389,28 @@ private function getId()
return ($formIdPrefix ?? 'inp-') . $name . ($render === 'radio' ? '-' . $value : '');
}

private function hasOldInput()
protected function hasOldInput()
{
return count((array) old()) != 0;
}

private function getValue()
protected function getValue()
{
extract($this->get('name', 'value', 'formData'));
if ($this->isRadioOrCheckbox()) {
return $value;
}

if ($this->hasOldInput()) {
return old($name, $value);
return old(preg_replace("/\\[\\]/mui","",$name), $value);
}

$fromFill = $formData[$name] ?? null;

return $value ?? $fromFill;
}

private function buildHtmlAttrs(array $attributes, $appendAttrs = true): string
protected function buildHtmlAttrs(array $attributes, $appendAttrs = true): string
{

if ($appendAttrs) {
Expand All @@ -432,7 +436,7 @@ private function buildHtmlAttrs(array $attributes, $appendAttrs = true): string
));
}

private function createAttrsList(...$items)
protected function createAttrsList(...$items)
{
$attrs = [];
foreach ($items as $item) {
Expand All @@ -444,7 +448,7 @@ private function createAttrsList(...$items)
return join(' ', array_filter($attrs));
}

private function errors()
protected function errors()
{
$errors = session('errors', app(ViewErrorBag::class));
extract($this->get('formErrorBag'));
Expand All @@ -454,7 +458,7 @@ private function errors()
return $errors;
}

private function get(...$keys): array
protected function get(...$keys): array
{
$return = [];
foreach ($keys as $key) {
Expand Down