Skip to content

Commit

Permalink
Fix data binding with square brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed May 29, 2024
1 parent 623b7e2 commit 8d3fcef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ public function getBoundValue($name, $default = null)
return value($default);
}

// Convert bracket-notation to dot-notation
$name = preg_replace('/\[([^]]+)]/', '.$1', $name);

// If old input is set, use that
if ($this->session_store && ($old = $this->session_store->getOldInput()) && Arr::has($old, $name)) {
return Arr::get($old, $name) ?? '';
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/DataBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ public function test_backed_enums_are_bound_propery(): void

$this->assertSelectorAttribute($input, 'input', 'value', 'Chris');
}

public function test_bound_data_with_square_brackets_is_supported(): void
{
$form = $this->aire()->form();

$form->bind(['foo' => ['bar' => 'baz']]);

$this->assertEquals('baz', $form->getBoundValue('foo[bar]'));
$this->assertEquals('baz', $form->getBoundValue('foo.bar'));
}
}

class ModelStub extends Model
Expand Down

0 comments on commit 8d3fcef

Please sign in to comment.