diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php
index a3632c8..de1c191 100644
--- a/src/Eloquent/FormAccessible.php
+++ b/src/Eloquent/FormAccessible.php
@@ -41,10 +41,41 @@ trait FormAccessible
return $this->mutateFormAttribute($key, $value);
}
+ $keys = explode('.', $key);
+
+ if ($this->isNestedModel($keys[0])) {
+ $relatedModel = $this->getRelation($keys[0]);
+
+ unset($keys[0]);
+ $key = implode('.', $keys);
+
+ if ($this->hasFormMutator($key)) {
+ return $relatedModel->getFormValue($key);
+ }
+
+ return data_get($relatedModel, $key);
+ }
+
// No form mutator, let the model resolve this
return data_get($this, $key);
}
+ /**
+ * Check for a nested model.
+ *
+ * @param string $key
+ *
+ * @return bool
+ */
+ public function isNestedModel($key)
+ {
+ if (in_array($key, array_keys($this->getRelations()))) {
+ return true;
+ }
+
+ return false;
+ }
+
/**
* @param $key
*
diff --git a/src/FormBuilder.php b/src/FormBuilder.php
index c225569..07d14de 100644
--- a/src/FormBuilder.php
+++ b/src/FormBuilder.php
@@ -90,6 +90,14 @@ class FormBuilder
*/
protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
+
+ /**
+ * Input Type.
+ *
+ * @var null
+ */
+ protected $type = null;
+
/**
* Create a new form builder instance.
*
@@ -255,6 +263,8 @@ class FormBuilder
*/
public function input($type, $name, $value = null, $options = [])
{
+ $this->type = $type;
+
if (! isset($options['name'])) {
$options['name'] = $name;
}
@@ -467,6 +477,8 @@ class FormBuilder
*/
public function textarea($name, $value = null, $options = [])
{
+ $this->type = 'textarea';
+
if (! isset($options['name'])) {
$options['name'] = $name;
}
@@ -545,6 +557,8 @@ class FormBuilder
array $selectAttributes = [],
array $optionsAttributes = []
) {
+ $this->type = 'select';
+
// When building a select box the "value" attribute is really the selected one
// so we will use that when checking the model or session for a value which
// should provide a convenient method of re-populating the forms on post.
@@ -777,6 +791,8 @@ class FormBuilder
*/
protected function checkable($type, $name, $value, $checked, $options)
{
+ $this->type = $type;
+
$checked = $this->getCheckedState($type, $name, $value, $checked);
if ($checked) {
@@ -1096,15 +1112,21 @@ class FormBuilder
return $value;
}
- if (! is_null($this->old($name)) && $name != '_method') {
- return $this->old($name);
+ $old = $this->old($name);
+
+ if (! is_null($old) && $name != '_method') {
+ return $old;
}
if (function_exists('app')) {
$hasNullMiddleware = app("Illuminate\Contracts\Http\Kernel")
->hasMiddleware(ConvertEmptyStringsToNull::class);
- if ($hasNullMiddleware && is_null($this->old($name)) && $this->view->shared('errors')->count() > 0) {
+ if ($hasNullMiddleware
+ && is_null($old)
+ && !is_null($this->view->shared('errors'))
+ && count($this->view->shared('errors')) > 0
+ ) {
return null;
}
}
@@ -1128,28 +1150,13 @@ class FormBuilder
*/
protected function getModelValueAttribute($name, $model = null)
{
- if (!isset($model)) {
- $model = $this->model;
- }
-
$key = $this->transformKey($name);
- // Check for nested attribute
- if (strpos($key, '.') !== false) {
- $keys = explode('.', $key, 2);
-
- // get the first level
- $_model = $this->getModelValueAttribute($keys[0], $model);
-
- // recursively get the next levels
- return $this->getModelValueAttribute($keys[1], $_model);
+ if (method_exists($this->model, 'getFormValue')) {
+ return $this->model->getFormValue($key);
}
- if (method_exists($model, 'getFormValue')) {
- return $model->getFormValue($key);
- }
-
- return data_get($model, $key);
+ return data_get($this->model, $this->transformKey($name));
}
/**
@@ -1162,7 +1169,25 @@ class FormBuilder
public function old($name)
{
if (isset($this->session)) {
- return $this->session->getOldInput($this->transformKey($name));
+ $key = $this->transformKey($name);
+ $payload = $this->session->getOldInput($key);
+
+ if (!is_array($payload)) {
+ return $payload;
+ }
+
+ if (!in_array($this->type, ['select', 'checkbox'])) {
+ if (!isset($this->payload[$key])) {
+ $this->payload[$key] = collect($payload);
+ }
+
+ if (!empty($this->payload[$key])) {
+ $value = $this->payload[$key]->shift();
+ return $value;
+ }
+ }
+
+ return $payload;
}
}
diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php
index c74d822..71e8df9 100755
--- a/src/HtmlServiceProvider.php
+++ b/src/HtmlServiceProvider.php
@@ -38,7 +38,7 @@ class HtmlServiceProvider extends ServiceProvider
$this->app->alias('html', HtmlBuilder::class);
$this->app->alias('form', FormBuilder::class);
- $this->registerBladeExtensions();
+ $this->registerBladeDirectives();
}
/**
@@ -72,7 +72,7 @@ class HtmlServiceProvider extends ServiceProvider
*
* @return void
*/
- protected function registerBladeExtensions()
+ protected function registerBladeDirectives()
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$namespaces = [
diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php
index 8aa0cee..4cfabda 100644
--- a/tests/FormAccessibleTest.php
+++ b/tests/FormAccessibleTest.php
@@ -50,11 +50,15 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
{
$model = new ModelThatUsesForms($this->modelData);
$relatedModel = new ModelThatUsesForms($this->modelData);
+ $relatedModel->address = [
+ 'street' => '123 Evergreen Terrace'
+ ];
$model->setRelation('related', $relatedModel);
+
$this->formBuilder->setModel($model);
$this->assertEquals($this->formBuilder->getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
- $this->assertEquals($this->formBuilder->getValueAttribute('related[created_at]'), $this->now->timestamp);
+ $this->assertEquals($this->formBuilder->getValueAttribute('related[address][street]'), '123 Evergreen Terrace');
}
public function testItCanGetRelatedValueForForms()
diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php
index eeea737..e06cc60 100644
--- a/tests/FormBuilderTest.php
+++ b/tests/FormBuilderTest.php
@@ -112,12 +112,21 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
$this->assertEquals('', $form4);
}
+ public function testFormTextArray()
+ {
+ $form1 = $this->formBuilder->input('text', 'foo[]', 'testing');
+ $form2 = $this->formBuilder->text('foo[]');
+
+ $this->assertEquals('', $form1);
+ $this->assertEquals('', $form2);
+ }
+
public function testFormTextRepopulation()
{
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
$this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']);
- $session->shouldReceive('getOldInput')->twice()->with('name_with_dots')->andReturn('some value');
+ $session->shouldReceive('getOldInput')->once()->with('name_with_dots')->andReturn('some value');
$input = $this->formBuilder->text('name.with.dots', 'default value');
$this->assertEquals('', $input);
@@ -348,12 +357,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
$this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']);
- $session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M');
+ $session->shouldReceive('getOldInput')->once()->with('size')->andReturn('M');
$select = $this->formBuilder->select('size', $list, 'S');
$this->assertEquals($select,
'');
-
- $session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']);
+// its likely that the mock has been changed so its not replicating correctly anymore
+ $session->shouldReceive('getOldInput')->once()->with('size.multi')->andReturn(['L', 'S']);
$select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']);
$this->assertEquals($select,
'');