From 45fe4e7988fa53773167dc9b6d39f32916f45661 Mon Sep 17 00:00:00 2001 From: Victor Isadov Date: Sat, 13 Aug 2016 11:57:01 +0300 Subject: [PATCH] Add Request support --- src/FormBuilder.php | 17 ++++++++++++++++- tests/FormBuilderTest.php | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 4e7cf0f..565931d 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 = []; + public $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; } /** @@ -1088,6 +1092,12 @@ class FormBuilder return $this->old($name); } + + $request_value = $this->request($name); + if (!is_null($request_value)) { + return $request_value; + } + if (! is_null($value)) { return $value; } @@ -1097,6 +1107,11 @@ class FormBuilder } } + protected function request($name) + { + return $this->request->input($this->transformKey($name)); + } + /** * Get the model value that should be assigned to the field. * diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 3e4eaad..babf01d 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -12,6 +12,11 @@ use Mockery as m; class FormBuilderTest extends PHPUnit_Framework_TestCase { + /** + * @var FormBuilder + */ + protected $formBuilder; + /** * Setup the test environment. */ @@ -20,7 +25,16 @@ 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'); + + $request = Request::create('/foo', 'GET', [ + "text" => [ + "value" => 1 + ], + ]); + + $request = Request::createFromBase($request); + + $this->formBuilder = new FormBuilder($this->htmlBuilder, $this->urlGenerator, $this->viewFactory, 'abc', $request); } /** @@ -31,6 +45,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase m::close(); } + public function testRequestValue() + { + $text = $this->formBuilder->text("text[value]"); + $this->assertEquals('', $text); + } + public function testOpeningForm() { $form1 = $this->formBuilder->open(['method' => 'GET']);