@@ -49,6 +49,12 @@ class FormBuilder
49
49
*/
50
50
protected $ csrfToken ;
51
51
52
+ /**
53
+ * Consider Request variables while auto fill.
54
+ * @var bool
55
+ */
56
+ protected $ considerRequest = false ;
57
+
52
58
/**
53
59
* The session store implementation.
54
60
*
@@ -108,6 +114,7 @@ class FormBuilder
108
114
* @param \Illuminate\Contracts\Routing\UrlGenerator $url
109
115
* @param \Illuminate\Contracts\View\Factory $view
110
116
* @param string $csrfToken
117
+ * @param Request $request
111
118
*/
112
119
public function __construct (HtmlBuilder $ html , UrlGenerator $ url , Factory $ view , $ csrfToken , Request $ request = null )
113
120
{
@@ -190,7 +197,7 @@ public function setModel($model)
190
197
{
191
198
$ this ->model = $ model ;
192
199
}
193
-
200
+
194
201
/**
195
202
* Get the current model instance on the form builder.
196
203
*
@@ -516,7 +523,7 @@ public function week($name, $value = null, $options = [])
516
523
517
524
return $ this ->input ('week ' , $ name , $ value , $ options );
518
525
}
519
-
526
+
520
527
/**
521
528
* Create a file input field.
522
529
*
@@ -730,7 +737,7 @@ public function selectMonth($name, $selected = null, $options = [], $format = '%
730
737
*/
731
738
public function getSelectOption ($ display , $ value , $ selected , array $ attributes = [], array $ optgroupAttributes = [])
732
739
{
733
- if (is_array ($ display )) {
740
+ if (is_iterable ($ display )) {
734
741
return $ this ->optionGroup ($ display , $ value , $ selected , $ optgroupAttributes , $ attributes );
735
742
}
736
743
@@ -755,7 +762,7 @@ protected function optionGroup($list, $label, $selected, array $attributes = [],
755
762
$ space = str_repeat (" " , $ level );
756
763
foreach ($ list as $ value => $ display ) {
757
764
$ optionAttributes = $ optionsAttributes [$ value ] ?? [];
758
- if (is_array ($ display )) {
765
+ if (is_iterable ($ display )) {
759
766
$ html [] = $ this ->optionGroup ($ display , $ value , $ selected , $ attributes , $ optionAttributes , $ level +5 );
760
767
} else {
761
768
$ html [] = $ this ->option ($ space .$ display , $ value , $ selected , $ optionAttributes );
@@ -805,7 +812,7 @@ protected function placeholderOption($display, $selected)
805
812
'value ' => '' ,
806
813
];
807
814
808
- return $ this ->toHtmlString ('<option ' . $ this ->html ->attributes ($ options ) . ' hidden="hidden" > ' . e ($ display , false ) . '</option> ' );
815
+ return $ this ->toHtmlString ('<option ' . $ this ->html ->attributes ($ options ) . '> ' . e ($ display , false ) . '</option> ' );
809
816
}
810
817
811
818
/**
@@ -907,7 +914,7 @@ protected function getCheckedState($type, $name, $value, $checked)
907
914
return $ this ->getRadioCheckedState ($ name , $ value , $ checked );
908
915
909
916
default :
910
- return $ this ->getValueAttribute ($ name) === $ value ;
917
+ return $ this ->compareValues ($ name, $ value) ;
911
918
}
912
919
}
913
920
@@ -960,7 +967,21 @@ protected function getRadioCheckedState($name, $value, $checked)
960
967
return $ checked ;
961
968
}
962
969
963
- return $ this ->getValueAttribute ($ name ) === $ value ;
970
+ return $ this ->compareValues ($ name , $ value );
971
+ }
972
+
973
+ /**
974
+ * Determine if the provide value loosely compares to the value assigned to the field.
975
+ * Use loose comparison because Laravel model casting may be in affect and therefore
976
+ * 1 == true and 0 == false.
977
+ *
978
+ * @param string $name
979
+ * @param string $value
980
+ * @return bool
981
+ */
982
+ protected function compareValues ($ name , $ value )
983
+ {
984
+ return $ this ->getValueAttribute ($ name ) == $ value ;
964
985
}
965
986
966
987
/**
@@ -1276,7 +1297,7 @@ public function getValueAttribute($name, $value = null)
1276
1297
if ($ hasNullMiddleware
1277
1298
&& is_null ($ old )
1278
1299
&& is_null ($ value )
1279
- && ! is_null ($ this ->view ->shared ('errors ' ))
1300
+ && !is_null ($ this ->view ->shared ('errors ' ))
1280
1301
&& count ($ this ->view ->shared ('errors ' )) > 0
1281
1302
) {
1282
1303
return null ;
@@ -1297,14 +1318,27 @@ public function getValueAttribute($name, $value = null)
1297
1318
}
1298
1319
}
1299
1320
1321
+ /**
1322
+ * Take Request in fill process
1323
+ * @param bool $consider
1324
+ */
1325
+ public function considerRequest ($ consider = true )
1326
+ {
1327
+ $ this ->considerRequest = $ consider ;
1328
+ }
1329
+
1300
1330
/**
1301
1331
* Get value from current Request
1302
1332
* @param $name
1303
1333
* @return array|null|string
1304
1334
*/
1305
1335
protected function request ($ name )
1306
1336
{
1307
- if (! isset ($ this ->request )) {
1337
+ if (!$ this ->considerRequest ) {
1338
+ return null ;
1339
+ }
1340
+
1341
+ if (!isset ($ this ->request )) {
1308
1342
return null ;
1309
1343
}
1310
1344
@@ -1342,16 +1376,16 @@ public function old($name)
1342
1376
$ key = $ this ->transformKey ($ name );
1343
1377
$ payload = $ this ->session ->getOldInput ($ key );
1344
1378
1345
- if (! is_array ($ payload )) {
1379
+ if (!is_array ($ payload )) {
1346
1380
return $ payload ;
1347
1381
}
1348
1382
1349
- if (! in_array ($ this ->type , ['select ' , 'checkbox ' ])) {
1350
- if (! isset ($ this ->payload [$ key ])) {
1383
+ if (!in_array ($ this ->type , ['select ' , 'checkbox ' ])) {
1384
+ if (!isset ($ this ->payload [$ key ])) {
1351
1385
$ this ->payload [$ key ] = collect ($ payload );
1352
1386
}
1353
1387
1354
- if (! empty ($ this ->payload [$ key ])) {
1388
+ if (!empty ($ this ->payload [$ key ])) {
1355
1389
$ value = $ this ->payload [$ key ]->shift ();
1356
1390
return $ value ;
1357
1391
}
0 commit comments