Add Request support

This commit is contained in:
Victor Isadov committed 2016-08-13 11:57:01 +03:00
1 parent abec449ed8
commit 45fe4e7988
2 files changed
+37 -2

No files matched your search

+16 -1
View File
@@ -4,6 +4,7 @@ namespace Collective\Html;
use DateTime; use DateTime;
use BadMethodCallException; use BadMethodCallException;
use Illuminate\Http\Request;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString; use Illuminate\Support\HtmlString;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
@@ -67,6 +68,8 @@ class FormBuilder
*/ */
protected $labels = []; protected $labels = [];
public $request;
/** /**
* The reserved form open attributes. * The reserved form open attributes.
* *
@@ -96,12 +99,13 @@ class FormBuilder
* @param \Illuminate\Contracts\View\Factory $view * @param \Illuminate\Contracts\View\Factory $view
* @param string $csrfToken * @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->url = $url;
$this->html = $html; $this->html = $html;
$this->view = $view; $this->view = $view;
$this->csrfToken = $csrfToken; $this->csrfToken = $csrfToken;
$this->request = $request;
} }
/** /**
@@ -1088,6 +1092,12 @@ class FormBuilder
return $this->old($name); return $this->old($name);
} }
$request_value = $this->request($name);
if (!is_null($request_value)) {
return $request_value;
}
if (! is_null($value)) { if (! is_null($value)) {
return $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. * Get the model value that should be assigned to the field.
* *
+21 -1
View File
@@ -12,6 +12,11 @@ use Mockery as m;
class FormBuilderTest extends PHPUnit_Framework_TestCase class FormBuilderTest extends PHPUnit_Framework_TestCase
{ {
/**
* @var FormBuilder
*/
protected $formBuilder;
/** /**
* Setup the test environment. * 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->urlGenerator = new UrlGenerator(new RouteCollection(), Request::create('/foo', 'GET'));
$this->viewFactory = m::mock(Factory::class); $this->viewFactory = m::mock(Factory::class);
$this->htmlBuilder = new HtmlBuilder($this->urlGenerator, $this->viewFactory); $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(); m::close();
} }
public function testRequestValue()
{
$text = $this->formBuilder->text("text[value]");
$this->assertEquals('<input name="text[value]" type="text" value="1">', $text);
}
public function testOpeningForm() public function testOpeningForm()
{ {
$form1 = $this->formBuilder->open(['method' => 'GET']); $form1 = $this->formBuilder->open(['method' => 'GET']);