Skip to content

Commit 2660c4d

Browse files
committed
Handle prepend bug
1 parent 2ef6b54 commit 2660c4d

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

src/Elements/Concerns/HasOptions.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function setOptionList(array $values) : self
3939
*/
4040
public function prependEmptyOption($label, $empty_value = '') : self
4141
{
42-
$this->view_data['options']->prepend($label, $empty_value);
42+
$this->view_data['prepend_empty_option'] = (object) [
43+
'value' => $empty_value,
44+
'label' => $label,
45+
];
4346

4447
return $this;
4548
}

src/Elements/Form.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ public function bind($bound_data) : self
149149
*
150150
* @param \Illuminate\Database\Eloquent\Model $model
151151
* @param string $resource_name
152+
* @param array $prepend_parameters
152153
* @return \Galahad\Aire\Elements\Form
153154
*/
154-
public function resourceful(Model $model, $resource_name = null) : self
155+
public function resourceful(Model $model, $resource_name = null, $prepend_parameters = []) : self
155156
{
156157
$this->bind($model);
157158

@@ -160,10 +161,13 @@ public function resourceful(Model $model, $resource_name = null) : self
160161
}
161162

162163
if ($model->exists) {
163-
$this->action($this->url->route("{$resource_name}.update", $model));
164+
$parameters = $prepend_parameters;
165+
$parameters[] = $model;
166+
167+
$this->action($this->url->route("{$resource_name}.update", $parameters));
164168
$this->put();
165169
} else {
166-
$this->action($this->url->route("{$resource_name}.store"));
170+
$this->action($this->url->route("{$resource_name}.store", $prepend_parameters));
167171
$this->post();
168172
}
169173

tests/Feature/ResourcefulBindingTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public function test_provided_name_overrides_inferred_name() : void
5151

5252
$this->assertSelectorAttribute($html, 'form', 'action', URL::to('/foo/bar'));
5353
}
54+
55+
public function test_route_parameters_are_prepended_when_loading_route() : void
56+
{
57+
Route::post('/{foo}/users', function() {})->name('foo.users.store');
58+
59+
$model = new XResourcefulModel(['id' => 1]);
60+
61+
$html = $this->aire()->form()->resourceful($model, 'foo.users', ['baz'])->render();
62+
63+
$this->assertSelectorAttribute($html, 'form', 'action', URL::to('/baz/users'));
64+
}
5465
}
5566

5667
class XResourcefulModel extends Model

tests/Unit/SelectTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,20 @@ public function test_a_custom_empty_option_can_be_prepended() : void
6767
$this->assertSelectorTextEquals($html, 'option[value="roth"]', 'Philip Roth');
6868
$this->assertSelectorTextEquals($html, 'option[value="patchett"]', 'Ann Patchett');
6969
}
70+
71+
public function test_an_empty_option_can_be_prepended_to_a_non_associative_array() : void
72+
{
73+
$options = [
74+
0 => 'Ta-Nehisi Coates',
75+
1 => 'Philip Roth',
76+
2 => 'Ann Patchett',
77+
];
78+
79+
$html = $this->aire()->select($options)->prependEmptyOption('Empty')->render();
80+
81+
$this->assertSelectorTextEquals($html, 'option[value=""]', 'Empty');
82+
$this->assertSelectorTextEquals($html, 'option[value="0"]', 'Ta-Nehisi Coates');
83+
$this->assertSelectorTextEquals($html, 'option[value="1"]', 'Philip Roth');
84+
$this->assertSelectorTextEquals($html, 'option[value="2"]', 'Ann Patchett');
85+
}
7086
}

views/checkbox-group.blade.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33

44
<div {{ $attributes->wrapper }}>
55

6+
@isset($prepend_empty_option)
7+
<label {{ $attributes->label }}>
8+
<input
9+
{{ $attributes->except('id', 'value', 'checked') }}
10+
value="{{ $prepend_empty_option->value }}"
11+
{{ $attributes->isValue($prepend_empty_option->value) ? 'checked' : '' }}
12+
/>
13+
<span {{ $attributes->label_wrappwer }}>
14+
{{ $prepend_empty_option->label }}
15+
</span>
16+
</label>
17+
@endisset
18+
619
@foreach($options->getOptions() as $option_value => $option_label)
720

821
<label {{ $attributes->label }}>

views/radio-group.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22
<?php /** @var \Galahad\Aire\Support\OptionsCollection $options */ ?>
33

44
<div {{ $attributes->wrapper }}>
5+
6+
@isset($prepend_empty_option)
7+
<label {{ $attributes->label }}>
8+
<input
9+
{{ $attributes->except('id', 'value', 'checked') }}
10+
value="{{ $prepend_empty_option->value }}"
11+
{{ $attributes->isValue($prepend_empty_option->value) ? 'checked' : '' }}
12+
/>
13+
<span {{ $attributes->label_wrapper }}>
14+
{{ $prepend_empty_option->label }}
15+
</span>
16+
</label>
17+
@endisset
18+
519
@foreach($options->getOptions() as $option_value => $option_label)
620

721
<label {{ $attributes->label }}>

views/select.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
<select {{ $attributes->except('value') }}>
55

6+
@isset($prepend_empty_option)
7+
<option value="{{ $prepend_empty_option->value }}" {{ $attributes->isValue($prepend_empty_option->value) ? 'selected' : '' }}>
8+
{{ $prepend_empty_option->label }}
9+
</option>
10+
@endisset
11+
612
@foreach($options->getOptions() as $value => $label)
713

814
<option value="{{ $value }}" {{ $attributes->isValue($value) ? 'selected' : '' }}>

0 commit comments

Comments
 (0)