Skip to content

Commit 10a89a8

Browse files
committed
Laravel up to 5.6 compatibility
Default values
1 parent 0588348 commit 10a89a8

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ By default this package will append an asterisk `*` to the label of required fie
8888
BootFormm:open([ 'label_required_mark' => '(required)', 'group_required_class' => 'field-required' ])
8989
```
9090

91+
### Default values
92+
93+
You can force default values by passing the values to the `open` method :
94+
95+
```
96+
BootFormm:open([ 'values' => ['foo' => 'bar'] ])
97+
```
98+
99+
These values take precedence over the model values, but not the provided or the old/request ones.
100+
91101
## Usage
92102

93103
When used in a blade template enclose the helper methods inside an unescaped block: `{!! !!}`

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
],
2020
"require": {
2121
"php": ">=5.4.0",
22-
"illuminate/config": "5.0.x|5.1.x|5.2.x|5.3.x|5.4.x|5.5.x",
23-
"illuminate/session": "5.0.x|5.1.x|5.2.x|5.3.x|5.4.x|5.5.x",
24-
"illuminate/support": "5.0.x|5.1.x|5.2.x|5.3.x|5.4.x|5.5.x",
25-
"laravelcollective/html": "5.0.x|5.1.x|5.2.x|5.3.x|5.4.x|5.5.x"
22+
"illuminate/config": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*",
23+
"illuminate/session": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*",
24+
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*",
25+
"laravelcollective/html": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*"
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "~5.7",

src/BootstrapForm.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class BootstrapForm
2020
protected $html;
2121

2222
/**
23-
* Illuminate FormBuilder instance.
23+
* Custom FormBuilder instance.
2424
*
25-
* @var \Collective\Html\FormBuilder
25+
* @var \Bnb\BootstrapForm\FormBuilder
2626
*/
2727
protected $form;
2828

@@ -154,6 +154,11 @@ public function open(array $options = [])
154154
$this->setErrorBag($options['errorbag']);
155155
}
156156

157+
if (array_key_exists('values', $options)) {
158+
$this->form->setDefaultValues($options['values']);
159+
unset($options['values']);
160+
}
161+
157162
return $this->form->open($options);
158163
}
159164

@@ -1091,6 +1096,18 @@ protected function setErrorBag($errorBag)
10911096
$this->errorBag = $errorBag;
10921097
}
10931098

1099+
/**
1100+
* Set the errorBag used for validation
1101+
*
1102+
* @param $errorBag
1103+
*
1104+
* @return void
1105+
*/
1106+
protected function setDefaultValues($values)
1107+
{
1108+
$this->form->setDefaultValues($values);
1109+
}
1110+
10941111

10951112
/**
10961113
* Flatten arrayed field names to work with the validator, including removing "[]",

src/FormBuilder.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,47 @@
88

99
namespace Bnb\BootstrapForm;
1010

11-
use Illuminate\Support\Collection;
12-
1311
class FormBuilder extends \Collective\Html\FormBuilder
1412
{
15-
public function __construct(\Collective\Html\FormBuilder $form) {
16-
parent::__construct($form->html, $form->url, $form->view, $form->csrfToken, $form->request);
17-
}
18-
1913

2014
/**
21-
* Create a placeholder select element option.
15+
* Default values that prevail over models one (lower priority than old and request)
2216
*
23-
* @param $display
24-
* @param $selected
17+
* @var array
18+
*/
19+
protected $defaultValues = [];
20+
21+
22+
/**
23+
* FormBuilder constructor.
2524
*
26-
* @return \Illuminate\Support\HtmlString
25+
* @param \Collective\Html\FormBuilder $form
2726
*/
28-
protected function placeholderOption($display, $selected)
27+
public function __construct($form)
2928
{
30-
$selected = $this->getSelectedValue(null, $selected);
29+
parent::__construct($form->html, $form->url, $form->view, $form->csrfToken, $form->request);
30+
}
3131

32-
$options = [
33-
'selected' => $selected,
34-
'value' => ''
35-
];
3632

37-
return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
33+
public function getValueAttribute($name, $value = null)
34+
{
35+
if ($value === null && isset($this->defaultValues['name'])) {
36+
$value = $this->defaultValues['name'];
37+
}
38+
39+
return parent::getValueAttribute($name, $value);
3840
}
3941

42+
4043
/**
41-
* Determine if the value is selected.
44+
* Set the default values that prevail over model ones (useful for filter)
4245
*
43-
* @param string $value
44-
* @param string $selected
46+
* @param array $values
4547
*
46-
* @return null|string
48+
* @return void
4749
*/
48-
protected function getSelectedValue($value, $selected)
50+
public function setDefaultValues(array $values)
4951
{
50-
if (is_array($selected)) {
51-
return in_array($value, $selected, true) || in_array((string) $value, $selected, true) ? 'selected' : null;
52-
} elseif ($selected instanceof Collection) {
53-
return $selected->contains($value) ? 'selected' : null;
54-
}
55-
56-
return ((string) $value == (string) $selected) ? 'selected' : null;
52+
$this->defaultValues = $values;
5753
}
5854
}

0 commit comments

Comments
 (0)