Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.
Merged
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
Bugfix for 'old' nested data
  • Loading branch information
pascalbaljet committed Feb 15, 2021
commit 3abd239c01d75e194d5a752c7480cd38a8593edf
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-form-components` will be documented in this file

## 2.5.4 - 2020-02-15

- Bugfix for old nested data

## 2.5.3 - 2020-02-11

- Bugfix for setting radio elements as checked/default
Expand Down
11 changes: 11 additions & 0 deletions src/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,15 @@ protected function generateIdByName(): string
{
return "auto_id_" . $this->name;
}

/**
* Converts a bracket-notation to a dotted-notation
*
* @param string $name
* @return string
*/
protected static function convertBracketsToDots($name): string
{
return str_replace(['[', ']'], ['.', ''], $name);
}
}
2 changes: 1 addition & 1 deletion src/Components/FormCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(

$inputName = Str::before($name, '[]');

if ($oldData = old($inputName)) {
if ($oldData = old(static::convertBracketsToDots($inputName))) {
$this->checked = in_array($value, Arr::wrap($oldData));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Components/FormErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FormErrors extends Component
*/
public function __construct(string $name, string $bag = 'default')
{
$this->name = str_replace(['[', ']'], ['.', ''], Str::before($name, '[]'));
$this->name = static::convertBracketsToDots(Str::before($name, '[]'));

$this->bag = $bag;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Components/FormRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function __construct(
$this->value = $value;
$this->showErrors = $showErrors;

if (old($name)) {
$this->checked = old($name) == $value;
$inputName = static::convertBracketsToDots($name);

if (old($inputName)) {
$this->checked = old($inputName) == $value;
}

if (!session()->hasOldInput() && $this->isNotWired()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Components/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(

$default = $this->getBoundValue($bind, $inputName) ?: $default;

$this->selectedKey = old($inputName, $default);
$this->selectedKey = old(static::convertBracketsToDots($inputName), $default);

if ($this->selectedKey instanceof Arrayable) {
$this->selectedKey = $this->selectedKey->toArray();
Expand Down
6 changes: 4 additions & 2 deletions src/Components/HandlesDefaultAndOldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ private function setValue(
return;
}

$inputName = static::convertBracketsToDots($name);

if (!$language) {
$default = $this->getBoundValue($bind, $name) ?: $default;

return $this->value = old($name, $default);
return $this->value = old($inputName, $default);
}

if ($bind !== false) {
Expand All @@ -30,6 +32,6 @@ private function setValue(
$default = $bind->getTranslation($name, $language, false) ?: $default;
}

$this->value = old("{$name}.{$language}", $default);
$this->value = old("{$inputName}.{$language}", $default);
}
}
28 changes: 28 additions & 0 deletions tests/Feature/BindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ public function it_overrides_the_bound_target_with_the_old_request_data()
->seeElement('input[name="radio"]:checked');
}

/** @test */
public function it_handles_old_nested_data()
{
$this->registerTestRoute('nested-validation-errors', function (Request $request) {
$request->validate([
'input.nested' => 'required',
'textarea.nested' => 'required',
'select.nested' => 'required',
'checkbox.nested' => 'required',
'radio.nested' => 'required',
]);
});

$this->visit('/nested-validation-errors')
->type('d', 'input[nested]')
->type('e', 'textarea[nested]')
->select('f', 'select[nested]')
->uncheck('checkbox[nested]')
->check('radio[nested]')
->press('Submit')
->seeElement('input[name="input[nested]"][value="d"]')
->seeInElement('textarea[name="textarea[nested]"]', 'e')
->seeElement('option[value="f"]:selected')
->seeElement('input[name="checkbox[nested]"]')
->dontSeeElement('input[name="checkbox[nested]"]:checked')
->seeElement('input[name="radio[nested]"]:checked');
}

/** @test */
public function it_overrides_the_default_value()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/views/nested-validation-errors.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-form>
<x-form-input name="input[nested]" />
<x-form-textarea name="textarea[nested]" />
<x-form-select name="select[nested]" :options="['c' => 'c', 'f' => 'f']" />
<x-form-checkbox name="checkbox[nested]" />

<x-form-group name="radio[nested]">
<x-form-radio name="radio[nested]" />
</x-form-group>

<x-form-submit />
</x-form>