Skip to content

Commit 6d1cceb

Browse files
Victor Isadovtshafer
authored andcommitted
Make Request optional (#522)
* Make Request optional * StyleCI
1 parent 42c0854 commit 6d1cceb

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

src/FormBuilder.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class FormBuilder
4949
*/
5050
protected $csrfToken;
5151

52+
/**
53+
* Consider Request variables while auto fill.
54+
* @var bool
55+
*/
56+
protected $considerRequest = false;
57+
5258
/**
5359
* The session store implementation.
5460
*
@@ -108,6 +114,7 @@ class FormBuilder
108114
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
109115
* @param \Illuminate\Contracts\View\Factory $view
110116
* @param string $csrfToken
117+
* @param Request $request
111118
*/
112119
public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null)
113120
{
@@ -1246,7 +1253,7 @@ public function getValueAttribute($name, $value = null)
12461253
if ($hasNullMiddleware
12471254
&& is_null($old)
12481255
&& is_null($value)
1249-
&& ! is_null($this->view->shared('errors'))
1256+
&& !is_null($this->view->shared('errors'))
12501257
&& count($this->view->shared('errors')) > 0
12511258
) {
12521259
return null;
@@ -1267,14 +1274,27 @@ public function getValueAttribute($name, $value = null)
12671274
}
12681275
}
12691276

1277+
/**
1278+
* Take Request in fill process
1279+
* @param bool $consider
1280+
*/
1281+
public function considerRequest($consider = true)
1282+
{
1283+
$this->considerRequest = $consider;
1284+
}
1285+
12701286
/**
12711287
* Get value from current Request
12721288
* @param $name
12731289
* @return array|null|string
12741290
*/
12751291
protected function request($name)
12761292
{
1277-
if (! isset($this->request)) {
1293+
if (!$this->considerRequest) {
1294+
return null;
1295+
}
1296+
1297+
if (!isset($this->request)) {
12781298
return null;
12791299
}
12801300

@@ -1312,16 +1332,16 @@ public function old($name)
13121332
$key = $this->transformKey($name);
13131333
$payload = $this->session->getOldInput($key);
13141334

1315-
if (! is_array($payload)) {
1335+
if (!is_array($payload)) {
13161336
return $payload;
13171337
}
13181338

1319-
if (! in_array($this->type, ['select', 'checkbox'])) {
1320-
if (! isset($this->payload[$key])) {
1339+
if (!in_array($this->type, ['select', 'checkbox'])) {
1340+
if (!isset($this->payload[$key])) {
13211341
$this->payload[$key] = collect($payload);
13221342
}
13231343

1324-
if (! empty($this->payload[$key])) {
1344+
if (!empty($this->payload[$key])) {
13251345
$value = $this->payload[$key]->shift();
13261346
return $value;
13271347
}

tests/FormBuilderTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function setUp()
3131
$request = Request::create('/foo', 'GET', [
3232
"person" => [
3333
"name" => "John",
34-
"surname" => "Doe"
34+
"surname" => "Doe",
3535
],
36-
"aggree" => 1,
37-
"checkbox_array" => [1,2,3]
36+
"agree" => 1,
37+
"checkbox_array" => [1, 2, 3],
3838
]);
3939

4040
$request = Request::createFromBase($request);
@@ -52,25 +52,33 @@ public function tearDown()
5252

5353
public function testRequestValue()
5454
{
55-
$name = $this->formBuilder->text("person[name]");
56-
$surname = $this->formBuilder->text("person[surname]");
55+
$this->formBuilder->considerRequest();
56+
$name = $this->formBuilder->text("person[name]", "Not John");
57+
$surname = $this->formBuilder->text("person[surname]", "Not Doe");
5758
$this->assertEquals('<input name="person[name]" type="text" value="John">', $name);
5859
$this->assertEquals('<input name="person[surname]" type="text" value="Doe">', $surname);
5960

60-
$checked = $this->formBuilder->checkbox("aggree", 1);
61+
$checked = $this->formBuilder->checkbox("agree", 1);
6162
$unchecked = $this->formBuilder->checkbox("no_value", 1);
62-
$this->assertEquals('<input checked="checked" name="aggree" type="checkbox" value="1">', $checked);
63+
$this->assertEquals('<input checked="checked" name="agree" type="checkbox" value="1">', $checked);
6364
$this->assertEquals('<input name="no_value" type="checkbox" value="1">', $unchecked);
6465

6566
$checked_array = $this->formBuilder->checkbox("checkbox_array[]", 1);
6667
$unchecked_array = $this->formBuilder->checkbox("checkbox_array[]", 4);
6768
$this->assertEquals('<input checked="checked" name="checkbox_array[]" type="checkbox" value="1">', $checked_array);
6869
$this->assertEquals('<input name="checkbox_array[]" type="checkbox" value="4">', $unchecked_array);
6970

70-
$checked = $this->formBuilder->radio("aggree", 1);
71+
$checked = $this->formBuilder->radio("agree", 1);
7172
$unchecked = $this->formBuilder->radio("no_value", 1);
72-
$this->assertEquals('<input checked="checked" name="aggree" type="radio" value="1">', $checked);
73+
$this->assertEquals('<input checked="checked" name="agree" type="radio" value="1">', $checked);
7374
$this->assertEquals('<input name="no_value" type="radio" value="1">', $unchecked);
75+
76+
// now we check that Request is ignored and value take precedence
77+
$this->formBuilder->considerRequest(false);
78+
$name = $this->formBuilder->text("person[name]", "Not John");
79+
$surname = $this->formBuilder->text("person[surname]", "Not Doe");
80+
$this->assertEquals('<input name="person[name]" type="text" value="Not John">', $name);
81+
$this->assertEquals('<input name="person[surname]" type="text" value="Not Doe">', $surname);
7482
}
7583

7684
public function testOpeningForm()

0 commit comments

Comments
 (0)