diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php index 9b5d6d0..eb74013 100644 --- a/src/Eloquent/FormAccessible.php +++ b/src/Eloquent/FormAccessible.php @@ -49,7 +49,7 @@ trait FormAccessible unset($keys[0]); $key = implode('.', $keys); - if ($this->hasFormMutator($key)) { + if (method_exists($relatedModel, 'hasFormMutator') && $relatedModel->hasFormMutator($key)) { return $relatedModel->getFormValue($key); } @@ -81,7 +81,7 @@ trait FormAccessible * * @return bool */ - protected function hasFormMutator($key) + public function hasFormMutator($key) { $methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC); diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 1c7e22d..94870b6 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -709,20 +709,23 @@ class FormBuilder * @param string $selected * @param array $attributes * @param array $optionsAttributes + * @param integer $level * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = []) + protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0) { $html = []; - + $space = str_repeat(" ", $level); foreach ($list as $value => $display) { $optionAttributes = $optionsAttributes[$value] ?? []; - - $html[] = $this->option($display, $value, $selected, $optionAttributes); + if (is_array($display)) { + $html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5); + } else { + $html[] = $this->option($space.$display, $value, $selected, $attributes, $optionAttributes); + } } - - return $this->toHtmlString('html->attributes($attributes) . '>' . implode('', $html) . ''); + return $this->toHtmlString('' . implode('', $html) . ''); } /** @@ -766,7 +769,7 @@ class FormBuilder 'value' => '', ]; - return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); + return $this->toHtmlString('html->attributes($options) . ' hidden="hidden">' . e($display) . ''); } /** diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 9f25686..2a1ef46 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -42,10 +42,13 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase public function testItCanMutateValuesForForms() { $model = new ModelThatUsesForms($this->modelData); + $user = new User(['name' => 'Anton']); + $model->setRelation('user', $user); $this->formBuilder->setModel($model); $this->assertEquals($model->getFormValue('string'), 'ponmlkjihgfedcba'); $this->assertEquals($model->getFormValue('created_at'), $this->now->timestamp); + $this->assertEquals('Get name for form: Anton', $model->getFormValue('user.name')); } public function testItCanMutateRelatedValuesForForms() @@ -159,3 +162,13 @@ class ModelThatDoesntUseForms extends Model return '1 second ago'; } } + +class User extends Model +{ + use FormAccessible; + + public function formNameAttribute($value) + { + return 'Get name for form: ' . $value; + } +} diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index e6b1765..47ff1b9 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -510,7 +510,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - ''); + ''); $select = $this->formBuilder->select( 'size', @@ -519,7 +519,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - ''); + ''); $select = $this->formBuilder->select( 'encoded_html', @@ -528,7 +528,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select the  '] ); $this->assertEquals($select, - '' + '' ); }