Skip to content

Commit

Permalink
Merge pull request #21 from kgrzelak/dev
Browse files Browse the repository at this point in the history
Old value from session update
  • Loading branch information
kgrzelak authored Jul 2, 2024
2 parents 3c73f75 + ad7609c commit 397d334
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/phpstan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
24 changes: 16 additions & 8 deletions src/BaseItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ public function value(mixed $value = null): static
$value = $value->value;
}

if (app('request')->old($this->attributes->getAttribute('name'))) {
$value = app('request')->old($this->attributes->getAttribute('name'));
}

$this->attributes->setAttribute('value', $value);

return $this;
Expand All @@ -134,10 +130,6 @@ public function setValue(mixed $value = null): static
$value = $value->value;
}

if (app('request')->old($this->attributes->getAttribute('name'))) {
$value = app('request')->old($this->attributes->getAttribute('name'));
}

$this->attributes->setAttribute('value', $value);

return $this;
Expand Down Expand Up @@ -236,6 +228,8 @@ public function toHtml(): string
$this->attributes->addClass(config('laravel-form.errors.element-class', 'is-invalid'));
}

$this->setOld();

return new HtmlString(
match ($this->viewName) {
'input' => (new InputRender())->render($this->attributes),
Expand Down Expand Up @@ -291,4 +285,18 @@ private function getErrors(): string

return $errors;
}

private function setOld(): void
{
$value = $this->attributes->getAttribute('value');

if (!$this->attributes->getAttribute('name')) {
return;
}

$this->attributes->setAttribute(
name: 'value',
value: app('session')->getOldInput($this->attributes->getAttribute('name'), $value)
);
}
}
22 changes: 22 additions & 0 deletions tests/FormInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,26 @@ public function testCanCreateInputWithoutRequired(): void

$this->assertEquals('<input type="text" class="form-control">', $item);
}

public function testCanCreateInputWithOldValues()
{
$this->setInputs();

$item = LaravelForm::input()->name('test');

$this->assertEquals('<input type="text" name="test" value="test" class="form-control">', $item);
}

public function testCanCreateInputWithOldValuesAndErrors()
{
$this->setInputs();
$this->setErrors();

$item = LaravelForm::input()->name('test');

$this->assertEquals(
expected: '<input type="text" name="test" value="test" class="form-control is-invalid"><span class="invalid-feedback" role="alert"><strong>error</strong></span>',
actual: $item
);
}
}
11 changes: 10 additions & 1 deletion tests/FormTextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ public function testCanCreateTextareaWithErrors(): void

public function testCanCreateTextareaWithValue(): void
{
$item = LaravelForm::textarea()->value('test');
$item = LaravelForm::textarea()->value('test')->toHtml();

$this->assertEquals('<textarea class="form-control">test</textarea>', $item);
}

public function testCanCreateTextareaWithOldValue(): void
{
$this->setInputs();

$item = LaravelForm::textarea()->name('test');

$this->assertEquals('<textarea name="test" class="form-control">test</textarea>', $item);
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public function setErrors(): void
$this->startSession();
$this->withSession(['errors' => (new ViewErrorBag)->put('default', new MessageBag(['test' => 'error']))]);
}

public function setInputs(): void
{
$this->startSession();
app('session')->flashInput(['test' => 'test']);
}
}

0 comments on commit 397d334

Please sign in to comment.