Skip to content

Commit

Permalink
Support old values for nested form fields
Browse files Browse the repository at this point in the history
  • Loading branch information
imacrayon committed Sep 8, 2020
1 parent a6dfc56 commit 21e671f
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/editors/easy-mde.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
name="{{ $name }}"
id="{{ $id }}"
{{ $attributes }}
>{{ old($name, $slot) }}</textarea>
>{{ old($dotName, $slot) }}</textarea>
2 changes: 1 addition & 1 deletion resources/views/components/editors/trix.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div {{ $attributes }}>
<input name="{{ $name }}" id="{{ $id }}" value="{{ old($name, $slot) }}" type="hidden">
<input name="{{ $name }}" id="{{ $id }}" value="{{ old($dotName, $slot) }}" type="hidden">
<trix-editor input="{{ $id }}" class="{{ $styling }}"></trix-editor>
</div>
2 changes: 1 addition & 1 deletion resources/views/components/forms/inputs/textarea.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
id="{{ $id }}"
rows="{{ $rows }}"
{{ $attributes }}
>{{ old($name, $slot) }}</textarea>
>{{ old($dotName, $slot) }}</textarea>
4 changes: 4 additions & 0 deletions src/Components/Editors/EasyMDE.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class EasyMDE extends BladeComponent
/** @var string */
public $name;

/** @var string */
public $dotName;

/** @var string */
public $id;

Expand All @@ -25,6 +28,7 @@ public function __construct(string $name, string $id = null, array $options = []
$this->name = $name;
$this->id = $id ?? $name;
$this->options = $options;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function options(): array
Expand Down
4 changes: 4 additions & 0 deletions src/Components/Editors/Trix.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Trix extends BladeComponent
/** @var string */
public $name;

/** @var string */
public $dotName;

/** @var string */
public $id;

Expand All @@ -25,6 +28,7 @@ public function __construct(string $name, string $id = null, string $styling = '
$this->name = $name;
$this->id = $id ?? $name;
$this->styling = $styling;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function render(): View
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Forms/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Error extends BladeComponent

public function __construct(string $field, string $bag = 'default')
{
$this->field = $field;
$this->field = trim(str_replace(['[', ']', '..'], '.', $field), '.');
$this->bag = $bag;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Components/Forms/Inputs/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(string $name, string $id = null, bool $checked = fal
{
parent::__construct($name, $id, 'checkbox');

$this->checked = (bool) old($name, $checked);
$this->checked = (bool) old($this->dotName, $checked);
}

public function render(): View
Expand Down
6 changes: 5 additions & 1 deletion src/Components/Forms/Inputs/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Input extends BladeComponent
/** @var string */
public $name;

/** @var string */
public $dotName;

/** @var string */
public $id;

Expand All @@ -26,7 +29,8 @@ public function __construct(string $name, string $id = null, string $type = 'tex
$this->name = $name;
$this->id = $id ?? $name;
$this->type = $type;
$this->value = old($name, $value);
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
$this->value = old($this->dotName, $value);
}

public function render(): View
Expand Down
4 changes: 4 additions & 0 deletions src/Components/Forms/Inputs/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Textarea extends BladeComponent
/** @var string */
public $name;

/** @var string */
public $dotName;

/** @var string */
public $id;

Expand All @@ -23,6 +26,7 @@ public function __construct(string $name, string $id = null, $rows = 3)
$this->name = $name;
$this->id = $id ?? $name;
$this->rows = $rows;
$this->dotName = trim(str_replace(['[', ']', '..'], '.', $name), '.');
}

public function render(): View
Expand Down
12 changes: 12 additions & 0 deletions tests/Components/Editors/EasyMDETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public function editor_can_have_old_values()
$this->assertComponentRenders($expected, '<x-easy-mde name="about"/>');
}

/** @test */
public function nested_editor_can_have_old_values()
{
$this->flashOld(['profile.about' => 'About me text']);

$expected = <<<HTML
<textarea x-data x-init="new EasyMDE({ element: \$el , ...{&quot;forceSync&quot;:true} })" name="profile[about]" id="profile[about]">About me text</textarea>
HTML;

$this->assertComponentRenders($expected, '<x-easy-mde name="profile[about]"/>');
}

/** @test */
public function editor_can_have_options()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/Components/Editors/TrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ public function editor_can_have_old_values()

$this->assertComponentRenders($expected, '<x-trix name="about"/>');
}

/** @test */
public function nested_editor_can_have_old_values()
{
$this->flashOld(['profile.about' => 'About me text']);

$expected = <<<HTML
<div>
<input name="profile[about]" id="profile[about]" value="About me text" type="hidden">
<trix-editor input="profile[about]" class="trix-content">
</trix-editor>
</div>
HTML;

$this->assertComponentRenders($expected, '<x-trix name="profile[about]"/>');
}
}
14 changes: 14 additions & 0 deletions tests/Components/Forms/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ public function it_can_be_slotted()

$this->assertComponentRenders($expected, $template);
}

/** @test */
public function it_supports_field_names_in_array_notation()
{
$this->withViewErrors(['profile.first_name' => 'Incorrect first name.']);

$expected = <<<HTML
<div class="text-red-500">
Incorrect first name.
</div>
HTML;

$this->assertComponentRenders($expected, '<x-error field="profile[first_name]" class="text-red-500"/>');
}
}
11 changes: 11 additions & 0 deletions tests/Components/Forms/Inputs/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ public function inputs_can_have_old_values()
'<x-checkbox name="remember_me"/>'
);
}

/** @test */
public function nested_inputs_can_have_old_values()
{
$this->flashOld(['login.remember_me' => true]);

$this->assertComponentRenders(
'<input name="login[remember_me]" type="checkbox" id="login[remember_me]" checked />',
'<x-checkbox name="login[remember_me]"/>'
);
}
}
11 changes: 11 additions & 0 deletions tests/Components/Forms/Inputs/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ public function inputs_can_have_old_values()
'<x-email/>'
);
}

/** @test */
public function nested_inputs_can_have_old_values()
{
$this->flashOld(['profile.email' => 'Eloquent']);

$this->assertComponentRenders(
'<input name="profile[email]" type="email" id="profile[email]" value="Eloquent" />',
'<x-email name="profile[email]"/>'
);
}
}
11 changes: 11 additions & 0 deletions tests/Components/Forms/Inputs/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ public function inputs_can_have_old_values()
'<x-input name="search" />'
);
}

/** @test */
public function nested_inputs_can_have_old_values()
{
$this->flashOld(['filters.search' => 'Eloquent']);

$this->assertComponentRenders(
'<input name="filters[search]" type="text" id="filters[search]" value="Eloquent" />',
'<x-input name="filters[search]" />'
);
}
}
11 changes: 11 additions & 0 deletions tests/Components/Forms/Inputs/PasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ public function inputs_cannot_have_old_values()
'<x-password/>'
);
}

/** @test */
public function nested_inputs_cannot_have_old_values()
{
$this->flashOld(['login.password' => 'secret']);

$this->assertComponentRenders(
'<input name="login[password]" type="password" id="login[password]" />',
'<x-password name="login[password]"/>'
);
}
}
12 changes: 12 additions & 0 deletions tests/Components/Forms/Inputs/PikadayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ public function pikaday_can_have_old_values()

$this->assertComponentRenders($expected, '<x-pikaday name="birthday"/>');
}

/** @test */
public function nested_pikaday_can_have_old_values()
{
$this->flashOld(['profile.birthday' => '23/03/1989']);

$expected = <<<HTML
<input x-data x-init="new Pikaday({ field: \$el , ...{&quot;format&quot;:&quot;DD\/MM\/YYYY&quot;} })" name="profile[birthday]" type="text" id="profile[birthday]" placeholder="DD/MM/YYYY" value="23/03/1989" />
HTML;

$this->assertComponentRenders($expected, '<x-pikaday name="profile[birthday]"/>');
}
}
11 changes: 11 additions & 0 deletions tests/Components/Forms/Inputs/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ public function inputs_can_have_old_values()
'<x-textarea name="about"/>'
);
}

/** @test */
public function nested_inputs_can_have_old_values()
{
$this->flashOld(['profile.about' => 'About me text']);

$this->assertComponentRenders(
'<textarea name="profile[about]" id="profile[about]" rows="3">About me text</textarea>',
'<x-textarea name="profile[about]"/>'
);
}
}

0 comments on commit 21e671f

Please sign in to comment.