From 5220e76b5a211531ed78ba5c929c4f7f99733251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Fri, 12 Apr 2019 16:45:25 +0200 Subject: [PATCH 01/11] Add placeholder as first option to select --- resources/views/forms/groups/select.blade.php | 6 ++++- tests/Feature/FormViewTests/SelectTest.php | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/resources/views/forms/groups/select.blade.php b/resources/views/forms/groups/select.blade.php index ac816d68..79bc116b 100644 --- a/resources/views/forms/groups/select.blade.php +++ b/resources/views/forms/groups/select.blade.php @@ -12,7 +12,11 @@ 'errorsId' => $errorsId = $controlId . ($errorsSuffix ?? 'Errors'), ]) > - @include('kontour::forms.partials.options') + @include('kontour::forms.partials.options', [ + 'options' => (empty($placeholder) or collect($options)->flatten()->has('')) ? + $options : + array_merge(['' => $placeholder], $options) + ]) {{ $afterControl ?? '' }} @include('kontour::forms.partials.errors') diff --git a/tests/Feature/FormViewTests/SelectTest.php b/tests/Feature/FormViewTests/SelectTest.php index e14e16df..2062258c 100644 --- a/tests/Feature/FormViewTests/SelectTest.php +++ b/tests/Feature/FormViewTests/SelectTest.php @@ -157,4 +157,28 @@ public function test_autofocus() $this->assertRegExp('//', $output); } + + public function test_placeholder_becomes_first_option() + { + $output = View::make('kontour::forms.select', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'placeholder' => 'Select one', + ])->render(); + + $this->assertRegExp('/\s*Select one<\/option>/', $output); + } + + public function test_placeholder_does_not_replace_existing_blank_option() + { + $output = View::make('kontour::forms.select', [ + 'name' => 'test', + 'options' => ['' => 'Existing placeholder', 'a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'placeholder' => 'Select one', + ])->render(); + + $this->assertNotRegExp('/Select one<\/option>/', $output); + } } From 7ae629a499a2d37acabd357c460167da6ff3fafd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Fri, 12 Apr 2019 17:12:58 +0200 Subject: [PATCH 02/11] Add placeholder attribute to inputs --- .../views/forms/partials/inputAttributes.blade.php | 3 +++ tests/Feature/FormViewTests/InputAttributesTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/resources/views/forms/partials/inputAttributes.blade.php b/resources/views/forms/partials/inputAttributes.blade.php index dcad2c3f..acaff76f 100644 --- a/resources/views/forms/partials/inputAttributes.blade.php +++ b/resources/views/forms/partials/inputAttributes.blade.php @@ -9,6 +9,9 @@ @if(!empty($autofocusControlId) and $autofocusControlId == $controlId) autofocus @endif +@if(!empty($placeholder)) + placeholder="{{ $placeholder }}" +@endif @if(isset($controlAttributes) and is_iterable($controlAttributes)) @foreach($controlAttributes as $attributeName => $attributeValue) @include('kontour::partials.attribute') diff --git a/tests/Feature/FormViewTests/InputAttributesTest.php b/tests/Feature/FormViewTests/InputAttributesTest.php index e0b3cc04..db0ee225 100644 --- a/tests/Feature/FormViewTests/InputAttributesTest.php +++ b/tests/Feature/FormViewTests/InputAttributesTest.php @@ -156,4 +156,16 @@ public function test_other_element_can_have_autofocus() $this->assertNotRegExp('/autofocus/', $output); } + + public function test_can_have_placeholder() + { + $output = View::make('kontour::forms.partials.inputAttributes', [ + 'name' => 'testName', + 'controlId' => 'testId', + 'errors' => new MessageBag, + 'placeholder' => 'Placeholder text', + ])->render(); + + $this->assertRegExp('/placeholder="Placeholder text"/', $output); + } } From f3fc531f367f302e1bc227d7b4df03ba4b326853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:17:36 +0200 Subject: [PATCH 03/11] Handle list of disabled options --- resources/views/forms/partials/options.blade.php | 3 +++ tests/Feature/FormViewTests/SelectTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/resources/views/forms/partials/options.blade.php b/resources/views/forms/partials/options.blade.php index e9d873a6..c95732cd 100644 --- a/resources/views/forms/partials/options.blade.php +++ b/resources/views/forms/partials/options.blade.php @@ -8,6 +8,9 @@ @if(is_array($selected) ? in_array(strval($option_value), $selected) : $selected == strval($option_value)) selected @endif + @if(!empty($disabled) and is_array($disabled) and in_array(strval($option_value), $disabled)) + disabled + @endif >{{ $option_display }} @endforeach @if($optgroup) diff --git a/tests/Feature/FormViewTests/SelectTest.php b/tests/Feature/FormViewTests/SelectTest.php index 2062258c..37124841 100644 --- a/tests/Feature/FormViewTests/SelectTest.php +++ b/tests/Feature/FormViewTests/SelectTest.php @@ -181,4 +181,17 @@ public function test_placeholder_does_not_replace_existing_blank_option() $this->assertNotRegExp('/Select one<\/option>/', $output); } + + public function test_options_can_be_disabled() + { + $output = View::make('kontour::forms.select', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'disabled' => ['b'], + ])->render(); + + $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); + $this->assertRegExp('/]*value="b"[^>]*disabled[^>]*>/', $output); + } } From b218e1a1175d9f9dc7dc759188613c1ebd64004e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:19:27 +0200 Subject: [PATCH 04/11] Test disabled options in multiselect --- tests/Feature/FormViewTests/MultiselectTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Feature/FormViewTests/MultiselectTest.php b/tests/Feature/FormViewTests/MultiselectTest.php index 660a8e44..7500e1b8 100644 --- a/tests/Feature/FormViewTests/MultiselectTest.php +++ b/tests/Feature/FormViewTests/MultiselectTest.php @@ -205,4 +205,17 @@ public function test_autofocus() $this->assertRegExp('//', $output); } + + public function test_options_can_be_disabled() + { + $output = View::make('kontour::forms.multiselect', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'disabled' => ['b'], + ])->render(); + + $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); + $this->assertRegExp('/]*value="b"[^>]*disabled[^>]*>/', $output); + } } From fc192aab04318bf12c57e5279f35247f3c0aaf88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:28:56 +0200 Subject: [PATCH 05/11] Handle list of disabled checkboxes --- .../views/forms/partials/checkableOption.blade.php | 3 +++ tests/Feature/FormViewTests/CheckboxesTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/resources/views/forms/partials/checkableOption.blade.php b/resources/views/forms/partials/checkableOption.blade.php index 9a3b2467..3c96e536 100644 --- a/resources/views/forms/partials/checkableOption.blade.php +++ b/resources/views/forms/partials/checkableOption.blade.php @@ -9,6 +9,9 @@ @if(is_array($selected) ? in_array(strval($option_value), $selected) : $selected == strval($option_value)) checked @endif + @if(!empty($disabled) and is_array($disabled) and in_array(strval($option_value), $disabled)) + disabled + @endif @include('kontour::forms.partials.inputAttributes', [ 'name' => is_array($selected) ? $name . '[]' : $name, 'autofocusControlId' => empty($autofocusControlId) ? null : ($autofocusControlId == $groupId ? $autofocusControlId . '.0' : $autofocusControlId), diff --git a/tests/Feature/FormViewTests/CheckboxesTest.php b/tests/Feature/FormViewTests/CheckboxesTest.php index 4bfbc582..043a2feb 100644 --- a/tests/Feature/FormViewTests/CheckboxesTest.php +++ b/tests/Feature/FormViewTests/CheckboxesTest.php @@ -210,4 +210,17 @@ public function test_autofocus_on_specific_option() $this->assertNotRegExp('/[\S\s]*assertRegExp('//', $output); } + + public function test_checkboxes_can_be_disabled() + { + $output = View::make('kontour::forms.checkboxes', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'disabled' => ['b'], + ])->render(); + + $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); + $this->assertRegExp('/]*value="b"[^>]*disabled[^>]*>/', $output); + } } From c0d770c415fa1b57acac1a2d83dbaa6c39dea67f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:30:33 +0200 Subject: [PATCH 06/11] Test that radiobuttons can be disabled --- tests/Feature/FormViewTests/RadiobuttonsTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Feature/FormViewTests/RadiobuttonsTest.php b/tests/Feature/FormViewTests/RadiobuttonsTest.php index 59fb51ec..71a8152c 100644 --- a/tests/Feature/FormViewTests/RadiobuttonsTest.php +++ b/tests/Feature/FormViewTests/RadiobuttonsTest.php @@ -157,4 +157,17 @@ public function test_autofocus_on_specific_option() $this->assertNotRegExp('/[\S\s]*assertRegExp('//', $output); } + + public function test_radiobuttons_can_be_disabled() + { + $output = View::make('kontour::forms.radiobuttons', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag, + 'disabled' => ['b'], + ])->render(); + + $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); + $this->assertRegExp('/]*value="b"[^>]*disabled[^>]*>/', $output); + } } From 29fb83182733f11655367ad61f530f3f846af182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:33:50 +0200 Subject: [PATCH 07/11] Rename disabled options list --- resources/views/forms/partials/checkableOption.blade.php | 2 +- resources/views/forms/partials/options.blade.php | 2 +- tests/Feature/FormViewTests/CheckboxesTest.php | 2 +- tests/Feature/FormViewTests/MultiselectTest.php | 2 +- tests/Feature/FormViewTests/RadiobuttonsTest.php | 2 +- tests/Feature/FormViewTests/SelectTest.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/resources/views/forms/partials/checkableOption.blade.php b/resources/views/forms/partials/checkableOption.blade.php index 3c96e536..4ddc1f84 100644 --- a/resources/views/forms/partials/checkableOption.blade.php +++ b/resources/views/forms/partials/checkableOption.blade.php @@ -9,7 +9,7 @@ @if(is_array($selected) ? in_array(strval($option_value), $selected) : $selected == strval($option_value)) checked @endif - @if(!empty($disabled) and is_array($disabled) and in_array(strval($option_value), $disabled)) + @if(!empty($disabledOptions) and is_array($disabledOptions) and in_array(strval($option_value), $disabledOptions)) disabled @endif @include('kontour::forms.partials.inputAttributes', [ diff --git a/resources/views/forms/partials/options.blade.php b/resources/views/forms/partials/options.blade.php index c95732cd..ebf9ca52 100644 --- a/resources/views/forms/partials/options.blade.php +++ b/resources/views/forms/partials/options.blade.php @@ -8,7 +8,7 @@ @if(is_array($selected) ? in_array(strval($option_value), $selected) : $selected == strval($option_value)) selected @endif - @if(!empty($disabled) and is_array($disabled) and in_array(strval($option_value), $disabled)) + @if(!empty($disabledOptions) and is_array($disabledOptions) and in_array(strval($option_value), $disabledOptions)) disabled @endif >{{ $option_display }} diff --git a/tests/Feature/FormViewTests/CheckboxesTest.php b/tests/Feature/FormViewTests/CheckboxesTest.php index 043a2feb..65cb59a1 100644 --- a/tests/Feature/FormViewTests/CheckboxesTest.php +++ b/tests/Feature/FormViewTests/CheckboxesTest.php @@ -217,7 +217,7 @@ public function test_checkboxes_can_be_disabled() 'name' => 'test', 'options' => ['a' => 'A', 'b' => 'B'], 'errors' => new MessageBag, - 'disabled' => ['b'], + 'disabledOptions' => ['b'], ])->render(); $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); diff --git a/tests/Feature/FormViewTests/MultiselectTest.php b/tests/Feature/FormViewTests/MultiselectTest.php index 7500e1b8..28a32928 100644 --- a/tests/Feature/FormViewTests/MultiselectTest.php +++ b/tests/Feature/FormViewTests/MultiselectTest.php @@ -212,7 +212,7 @@ public function test_options_can_be_disabled() 'name' => 'test', 'options' => ['a' => 'A', 'b' => 'B'], 'errors' => new MessageBag, - 'disabled' => ['b'], + 'disabledOptions' => ['b'], ])->render(); $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); diff --git a/tests/Feature/FormViewTests/RadiobuttonsTest.php b/tests/Feature/FormViewTests/RadiobuttonsTest.php index 71a8152c..1fb05ef3 100644 --- a/tests/Feature/FormViewTests/RadiobuttonsTest.php +++ b/tests/Feature/FormViewTests/RadiobuttonsTest.php @@ -164,7 +164,7 @@ public function test_radiobuttons_can_be_disabled() 'name' => 'test', 'options' => ['a' => 'A', 'b' => 'B'], 'errors' => new MessageBag, - 'disabled' => ['b'], + 'disabledOptions' => ['b'], ])->render(); $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); diff --git a/tests/Feature/FormViewTests/SelectTest.php b/tests/Feature/FormViewTests/SelectTest.php index 37124841..7e43fe51 100644 --- a/tests/Feature/FormViewTests/SelectTest.php +++ b/tests/Feature/FormViewTests/SelectTest.php @@ -188,7 +188,7 @@ public function test_options_can_be_disabled() 'name' => 'test', 'options' => ['a' => 'A', 'b' => 'B'], 'errors' => new MessageBag, - 'disabled' => ['b'], + 'disabledOptions' => ['b'], ])->render(); $this->assertNotRegExp('/]*value="a"[^>]*disabled[^>]*>/', $output); From 8fecf34113370e936df74284602f87776dc669fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 11:46:35 +0200 Subject: [PATCH 08/11] Document attribute parameters for form views --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 17d7e5cb..300d0096 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,12 @@ If you get an id conflict on a page where two inputs may have the same name, e.g. in different forms, different `$idPrefix` can be passed along to the templates to make the ids unique. +The form views take a `$controlAttributes` array that can be used to set any additional html attributes +on a form control element. +This can be useful to set `required`, `disabled`, `readonly`, `autocomplete`, and other attributes specific to the +[different input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form__types). +The corresponding parameter to put extra attributes on the label tag is `$labelAttributes`. + #### Input autofocus The variable `$autofocusControlId` can be set to the id of the input you want to `autofocus`, @@ -275,6 +281,7 @@ You could even set in the controller and pass it along to the view. [The button views](https://github.com/kontenta/kontour/tree/master/resources/views/buttons) generate buttom elements for common actions like "create", "update", and "destroy", as well as a "generic" button, and a "link"-like button. +The button views take a `$buttonAttributes` array of html attributes to set on the button element. ```php @component('kontour::buttons.generic', ['type' => 'reset']) From ed78eea7a045a261cc5951daff77d86bdd8f3233 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 12:06:58 +0200 Subject: [PATCH 09/11] Document options of available input templates --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 300d0096..db5b6551 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,19 @@ If no `$idPrefix` is set, this conveniently corresponds to the keys in Laravel's It's best to set it as high up as possible in the view, before any forms are included. You could even set in the controller and pass it along to the view. +### Available input templates + +All inputs take at least the `$name` parameter. + +- `input` - Just pass `$type` to set the input type (defaults to `text`). +- `textarea` +- `select` - Pass `$options` as an `array` of key-values and an optional `$selected` `string` and `$disabledOptions` `array`. +- `radiobuttons` - Same API as `select` for printing radiobuttons instead. +- `multiselect` - Same API as `select` but optional `$selected` `array` instead of `string`. +- `checkboxes` - Same API as `multiselect` for printing checkboxes instead. +- `checkbox` - Pass optional `$checked` as `boolean` and `$value` for a `value` attribute other than + default `1` (or `$checkbox_default_value`). + ### Button templates [The button views](https://github.com/kontenta/kontour/tree/master/resources/views/buttons) From 1086953a35235a07678fb58747d0dd7adeca0579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 12:08:25 +0200 Subject: [PATCH 10/11] Consistently name template parameters --- README.md | 2 +- resources/views/forms/elements/checkbox.blade.php | 2 +- tests/Feature/FormViewTests/CheckboxTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index db5b6551..4317d190 100644 --- a/README.md +++ b/README.md @@ -287,7 +287,7 @@ All inputs take at least the `$name` parameter. - `multiselect` - Same API as `select` but optional `$selected` `array` instead of `string`. - `checkboxes` - Same API as `multiselect` for printing checkboxes instead. - `checkbox` - Pass optional `$checked` as `boolean` and `$value` for a `value` attribute other than - default `1` (or `$checkbox_default_value`). + default `1` (or `$checkboxDefaultValue`). ### Button templates diff --git a/resources/views/forms/elements/checkbox.blade.php b/resources/views/forms/elements/checkbox.blade.php index cf6562b3..242f4334 100644 --- a/resources/views/forms/elements/checkbox.blade.php +++ b/resources/views/forms/elements/checkbox.blade.php @@ -2,6 +2,6 @@ @if(old($name, $checked ?? $model[$name] ?? false)) checked @endif - value="{{ $value ?? $checkbox_default_value ?? '1' }}" + value="{{ $value ?? $checkboxDefaultValue ?? '1' }}" @include('kontour::forms.partials.inputAttributes') > \ No newline at end of file diff --git a/tests/Feature/FormViewTests/CheckboxTest.php b/tests/Feature/FormViewTests/CheckboxTest.php index 10a7e501..01d44b8d 100644 --- a/tests/Feature/FormViewTests/CheckboxTest.php +++ b/tests/Feature/FormViewTests/CheckboxTest.php @@ -79,7 +79,7 @@ public function test_default_value_can_be_set() $output = View::make('kontour::forms.checkbox', [ 'name' => 'test', 'errors' => new MessageBag, - 'checkbox_default_value' => 2, + 'checkboxDefaultValue' => 2, ])->render(); $this->assertRegExp('//', $output); From cacbe9fc1a295ea242fe0ec6b8e592f8e8737f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 15 Apr 2019 12:25:31 +0200 Subject: [PATCH 11/11] Document common parameters --- README.md | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4317d190..ea9a77a6 100644 --- a/README.md +++ b/README.md @@ -262,12 +262,6 @@ If you get an id conflict on a page where two inputs may have the same name, e.g. in different forms, different `$idPrefix` can be passed along to the templates to make the ids unique. -The form views take a `$controlAttributes` array that can be used to set any additional html attributes -on a form control element. -This can be useful to set `required`, `disabled`, `readonly`, `autocomplete`, and other attributes specific to the -[different input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form__types). -The corresponding parameter to put extra attributes on the label tag is `$labelAttributes`. - #### Input autofocus The variable `$autofocusControlId` can be set to the id of the input you want to `autofocus`, @@ -276,12 +270,31 @@ If no `$idPrefix` is set, this conveniently corresponds to the keys in Laravel's It's best to set it as high up as possible in the view, before any forms are included. You could even set in the controller and pass it along to the view. -### Available input templates +### Common parameters -All inputs take at least the `$name` parameter. +All inputs need at least the `$name` parameter +and optional `$placeholder` and `$ariaDescribedById` parameters. + +All form views take a `$controlAttributes` `array` that can be used to set any additional html attributes +on the form control element. +This can be useful for setting `required`, `disabled`, `readonly`, `autocomplete`, and other attributes specific to the +[different input types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form__types). + +```php +
+@include('kontour::forms.input', [ + 'name' => 'country_code', + 'controlAttributes' => ['required', 'autocomplete' => 'country', 'size' => '2'] +]]) +
+``` + +The corresponding parameter to put extra attributes on the label tag is `$labelAttributes`. + +### Available input templates -- `input` - Just pass `$type` to set the input type (defaults to `text`). -- `textarea` +- `textarea` - Pass `$value` to set input contents. +- `input` - Same API as `textarea`, but you can pass `$type` to set the input type (defaults to `text`). - `select` - Pass `$options` as an `array` of key-values and an optional `$selected` `string` and `$disabledOptions` `array`. - `radiobuttons` - Same API as `select` for printing radiobuttons instead. - `multiselect` - Same API as `select` but optional `$selected` `array` instead of `string`.