diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 5ec62db..a844565 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -4,6 +4,7 @@ namespace Collective\Html; use DateTime; use BadMethodCallException; +use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Contracts\View\Factory; @@ -67,6 +68,8 @@ class FormBuilder */ protected $labels = []; + protected $request; + /** * The reserved form open attributes. * @@ -96,12 +99,13 @@ class FormBuilder * @param \Illuminate\Contracts\View\Factory $view * @param string $csrfToken */ - public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken) + public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null) { $this->url = $url; $this->html = $html; $this->view = $view; $this->csrfToken = $csrfToken; + $this->request = $request; } /** @@ -716,7 +720,7 @@ class FormBuilder protected function getSelectedValue($value, $selected) { if (is_array($selected)) { - return in_array($value, $selected) ? 'selected' : null; + return in_array($value, $selected, true) ? 'selected' : null; } return ((string) $value == (string) $selected) ? 'selected' : null; @@ -813,11 +817,13 @@ class FormBuilder */ protected function getCheckboxCheckedState($name, $value, $checked) { - if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name))) { + $request = $this->request($name); + + if (isset($this->session) && ! $this->oldInputIsEmpty() && is_null($this->old($name)) && !$request) { return false; } - if ($this->missingOldAndModel($name)) { + if ($this->missingOldAndModel($name) && !$request) { return $checked; } @@ -843,7 +849,9 @@ class FormBuilder */ protected function getRadioCheckedState($name, $value, $checked) { - if ($this->missingOldAndModel($name)) { + $request = $this->request($name); + + if ($this->missingOldAndModel($name) && !$request) { return $checked; } @@ -1092,6 +1100,11 @@ class FormBuilder return $this->old($name); } + $request = $this->request($name); + if (!is_null($request)) { + return $request; + } + if (! is_null($value)) { return $value; } @@ -1101,6 +1114,20 @@ class FormBuilder } } + /** + * Get value from current Request + * @param $name + * @return array|null|string + */ + protected function request($name) + { + if (!isset($this->request)) { + return null; + } + + return $this->request->input($this->transformKey($name)); + } + /** * Get the model value that should be assigned to the field. * @@ -1201,16 +1228,12 @@ class FormBuilder */ public function __call($method, $parameters) { - try { + if (static::hasComponent($method)) { return $this->componentCall($method, $parameters); - } catch (BadMethodCallException $e) { - // } - try { + if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); - } catch (BadMethodCallException $e) { - // } throw new BadMethodCallException("Method {$method} does not exist."); diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index e531792..2385103 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -441,11 +441,18 @@ class HtmlBuilder */ protected function attributeElement($key, $value) { - // For numeric keys we will assume that the key and the value are the same - // as this will convert HTML attributes such as "required" to a correct - // form like required="required" instead of using incorrect numerics. + // For numeric keys we will assume that the value is a boolean attribute + // where the presence of the attribute represents a true value and the + // absence represents a false value. + // This will convert HTML attributes such as "required" to a correct + // form instead of using incorrect numerics. if (is_numeric($key)) { - $key = $value; + return $value; + } + + // Treat boolean attributes as HTML properties + if (is_bool($value)) { + return $value ? $key : ''; } if (! is_null($value)) { @@ -546,16 +553,12 @@ class HtmlBuilder */ public function __call($method, $parameters) { - try { + if (static::hasComponent($method)) { return $this->componentCall($method, $parameters); - } catch (BadMethodCallException $e) { - // } - try { + if (static::hasMacro($method)) { return $this->macroCall($method, $parameters); - } catch (BadMethodCallException $e) { - // } throw new BadMethodCallException("Method {$method} does not exist."); diff --git a/src/HtmlServiceProvider.php b/src/HtmlServiceProvider.php index c632e56..e3aa134 100755 --- a/src/HtmlServiceProvider.php +++ b/src/HtmlServiceProvider.php @@ -49,7 +49,7 @@ class HtmlServiceProvider extends ServiceProvider protected function registerFormBuilder() { $this->app->singleton('form', function ($app) { - $form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken()); + $form = new FormBuilder($app['html'], $app['url'], $app['view'], $app['session.store']->getToken(), $app['request']); return $form->setSessionStore($app['session.store']); }); diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 9add106..3d29a4b 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -27,6 +27,8 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase 'address' => [ 'street' => 'abcde st' ], + 'array' => [1, 2, 3,], + 'transform_key' => 'testing testing', 'created_at' => $this->now, 'updated_at' => $this->now, ]; @@ -51,6 +53,31 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase $model = new ModelThatUsesForms($this->modelData); $this->assertEquals($model->getFormValue('address.street'), 'abcde st'); } + + public function testItCanUseGetAccessorValuesWhenThereAreNoFormAccessors() + { + $model = new ModelThatUsesForms($this->modelData); + $this->formBuilder->setModel($model); + + $this->assertEquals($this->formBuilder->getValueAttribute('email'), 'mutated@tjshafer.com'); + } + + public function testItReturnsSameResultWithAndWithoutThisFeature() + { + $modelWithAccessor = new ModelThatUsesForms($this->modelData); + $modelWithoutAccessor = new ModelThatDoesntUseForms($this->modelData); + + $this->formBuilder->setModel($modelWithAccessor); + $valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array'); + $valuesWithAccessor[] = $this->formBuilder->getValueAttribute('array[0]'); + $valuesWithAccessor[] = $this->formBuilder->getValueAttribute('transform.key'); + $this->formBuilder->setModel($modelWithoutAccessor); + $valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array'); + $valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('array[0]'); + $valuesWithoutAccessor[] = $this->formBuilder->getValueAttribute('transform.key'); + + $this->assertEquals($valuesWithAccessor, $valuesWithoutAccessor); + } public function testItCanStillMutateValuesForViews() { @@ -96,6 +123,11 @@ class ModelThatUsesForms extends Model { return '1 second ago'; } + + public function getEmailAttribute($value) + { + return 'mutated@tjshafer.com'; + } } class ModelThatDoesntUseForms extends Model diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 705673e..30cc095 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -11,6 +11,10 @@ use Mockery as m; class FormBuilderTest extends PHPUnit_Framework_TestCase { + /** + * @var FormBuilder + */ + protected $formBuilder; /** * Setup the test environment. @@ -20,7 +24,20 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET')); $this->viewFactory = m::mock(Factory::class); $this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory); - $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc'); + + // prepare request for test with some data + $request = Request::create('/foo', 'GET', [ + "person" => [ + "name" => "John", + "surname" => "Doe" + ], + "aggree" => 1, + "checkbox_array" => [1,2,3] + ]); + + $request = Request::createFromBase($request); + + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc', $request); } /** @@ -31,6 +48,29 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase m::close(); } + public function testRequestValue() + { + $name = $this->formBuilder->text("person[name]"); + $surname = $this->formBuilder->text("person[surname]"); + $this->assertEquals('', $name); + $this->assertEquals('', $surname); + + $checked = $this->formBuilder->checkbox("aggree", 1); + $unchecked = $this->formBuilder->checkbox("no_value", 1); + $this->assertEquals('', $checked); + $this->assertEquals('', $unchecked); + + $checked_array = $this->formBuilder->checkbox("checkbox_array[]", 1); + $unchecked_array = $this->formBuilder->checkbox("checkbox_array[]", 4); + $this->assertEquals('', $checked_array); + $this->assertEquals('', $unchecked_array); + + $checked = $this->formBuilder->radio("aggree", 1); + $unchecked = $this->formBuilder->radio("no_value", 1); + $this->assertEquals('', $checked); + $this->assertEquals('', $unchecked); + } + public function testOpeningForm() { $form1 = $this->formBuilder->open(['method' => 'GET']); @@ -265,6 +305,16 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals($select, ''); + $select = $this->formBuilder->select( + 'size', + ['0' => 'All Sizes', 'L' => 'Large', 'M' => 'Medium', 'S' => 'Small'], + ['M'], + ['multiple'] + ); + $this->assertEquals( + $select, + ''); + $select = $this->formBuilder->select( 'size', ['L' => 'Large', 'S' => 'Small'], @@ -545,6 +595,15 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('', $form3); } + public function testBooleanAttributes() + { + $input = $this->formBuilder->text('test', null, ['disabled']); + $this->assertEquals('', $input); + + $input = $this->formBuilder->textarea('test', null, ['readonly']); + $this->assertEquals('', $input); + } + protected function setModel(array $data, $object = true) { if ($object) { diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 3510c4e..b5d7d99 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -137,4 +137,15 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('<span>First Name Last</span>', $result1); $this->assertEquals('First Name Last', $result2); } + + public function testBooleanAttributes() + { + $result1 = $this->htmlBuilder->attributes(['my-property' => true]); + + $result2 = $this->htmlBuilder->attributes(['my-property' => false]); + + $this->assertEquals('my-property', trim($result1)); + + $this->assertEquals('', trim($result2)); + } }