Skip to content

Commit 09f4977

Browse files
committed
Add support for Bootstrap 4
1 parent f637db8 commit 09f4977

12 files changed

Lines changed: 124 additions & 106 deletions

config.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
// type => class or classes
7272
'default' => 'form-control',
7373
'checkbox' => '',
74-
'error' => 'input-with-feedback'
74+
'error' => 'error'
7575
],
7676
],
7777
/**
@@ -94,8 +94,8 @@
9494
'field_classes' => [
9595
// type => class or classes
9696
'default' => 'form-control',
97-
'checkbox' => '',
98-
'error' => 'input-with-feedback'
97+
'checkbox' => 'form-check-input',
98+
'error' => 'is-invalid'
9999
],
100100
]
101101
]

spec/FormBuilderSpec.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function it_generate_radios($theme)
4848
// Having
4949
$name = 'gender';
5050
$attributes = [];
51+
$hasErrors = false;
5152

5253
// Expect
5354
$radios = [
@@ -66,7 +67,7 @@ function it_generate_radios($theme)
6667
"id" => "gender_f"
6768
]
6869
];
69-
$theme->render(null, compact('name', 'radios', 'attributes'), "forms.radios")->shouldBeCalled();
70+
$theme->render(null, compact('name', 'radios', 'attributes', 'hasErrors'), "forms.radios")->shouldBeCalled();
7071

7172
// When
7273
$this->radios('gender', ['m' => 'Male', 'f' => 'Female'], 'm');
@@ -79,6 +80,7 @@ function it_generate_checkboxes($theme)
7980
$tags = ['php' => 'PHP', 'python' => 'Python', 'js' => 'JS', 'ruby' => 'Ruby on Rails'];
8081
$checked = ['php', 'js'];
8182
$attributes = [];
83+
$hasErrors = false;
8284

8385
// Expect
8486
$checkboxes = [
@@ -111,7 +113,7 @@ function it_generate_checkboxes($theme)
111113
"id" => "tags_ruby"
112114
]
113115
];
114-
$theme->render(null, compact('name', 'checkboxes', 'attributes'), "forms.checkboxes")->shouldBeCalled();
116+
$theme->render(null, compact('name', 'checkboxes', 'attributes', 'hasErrors'), "forms.checkboxes")->shouldBeCalled();
115117

116118
// When
117119
$this->checkboxes('tags', $tags, $checked);

src/FieldBuilder.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ protected function getClasses($type, array $attributes = [], $errors = null)
633633
}
634634

635635
if ( ! empty($errors)) {
636-
$classes .= ' '.(isset($classes['error']) ? $classes['error'] : 'error');
636+
$classes .= ' '.(isset($this->cssClasses['error']) ? $this->cssClasses['error'] : 'error');
637637
}
638638

639639
return trim($classes);
@@ -757,7 +757,7 @@ protected function doBuild($type, $name, $value = null, array $attributes = arra
757757

758758
$attributes = $this->getHtmlAttributes($type, $attributes, $errors, $id, $required);
759759

760-
$input = $this->buildControl($type, $name, $value, $attributes, $options, $htmlName);
760+
$input = $this->buildControl($type, $name, $value, $attributes, $options, $htmlName, $hasErrors);
761761

762762
return $this->theme->render(
763763
$customTemplate,
@@ -775,10 +775,11 @@ protected function doBuild($type, $name, $value = null, array $attributes = arra
775775
* @param array $attributes
776776
* @param array|null $options
777777
* @param string $htmlName
778-
*
778+
* @param bool $hasErrors
779779
* @return string
780+
*
780781
*/
781-
protected function buildControl($type, $name, $value, $attributes, $options, $htmlName)
782+
protected function buildControl($type, $name, $value, $attributes, $options, $htmlName, $hasErrors = false)
782783
{
783784
switch ($type) {
784785
case 'password':
@@ -801,7 +802,8 @@ protected function buildControl($type, $name, $value, $attributes, $options, $ht
801802
$htmlName,
802803
$this->getOptionsList($name, $options),
803804
$value,
804-
$attributes
805+
$attributes,
806+
$hasErrors
805807
);
806808
case 'checkbox':
807809
return $this->form->checkbox(

src/FormBuilder.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ public function time($name, $value = null, $options = array())
109109
* @param array $options
110110
* @param string $selected
111111
* @param array $attributes
112+
* @param bool $hasErrors
112113
*
113114
* @return string
114115
*/
115-
public function radios($name, $options = array(), $selected = null, $attributes = array())
116+
public function radios($name, $options = array(), $selected = null, $attributes = array(), $hasErrors = false)
116117
{
117118
$selected = $this->getValueAttribute($name, $selected);
118119

@@ -132,15 +133,15 @@ public function radios($name, $options = array(), $selected = null, $attributes
132133
'value' => $value,
133134
'label' => $label,
134135
'selected' => $selected == $value,
135-
'id' => $name.'_'.Str::slug($value)
136+
'id' => $name.'_'.Str::slug($value),
136137
];
137138
}
138139

139140
unset ($attributes['inline'], $attributes['template']);
140141

141142
return $this->theme->render(
142143
$template,
143-
compact('name', 'radios', 'attributes'),
144+
compact('name', 'radios', 'attributes', 'classes', 'hasErrors'),
144145
$defaultTemplate
145146
);
146147
}
@@ -161,10 +162,11 @@ public function radios($name, $options = array(), $selected = null, $attributes
161162
* @param array $options
162163
* @param string $selected
163164
* @param array $attributes
165+
* @param bool $hasErrors
164166
*
165167
* @return string
166168
*/
167-
public function checkboxes($name, $options = array(), $selected = null, $attributes = array())
169+
public function checkboxes($name, $options = array(), $selected = null, $attributes = array(), $hasErrors = false)
168170
{
169171
$selected = $this->getValueAttribute($name, $selected);
170172

@@ -196,7 +198,7 @@ public function checkboxes($name, $options = array(), $selected = null, $attribu
196198

197199
return $this->theme->render(
198200
$template,
199-
compact('name', 'checkboxes', 'attributes'),
201+
compact('name', 'checkboxes', 'attributes', 'hasErrors'),
200202
$defaultTemplate
201203
);
202204
}

themes/bootstrap4/alert.blade.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
@foreach ($messages as $msg)
2-
<div class="alert alert-{{ $msg['type'] }} alert-dismissible fade show">
2+
<div class="alert alert-{{ $msg['type'] }} alert-dismissible fade show" role="alert">
3+
<strong>{{ $msg['message'] }}</strong>
4+
@if (!empty ($msg['details']))
5+
{{ $msg['details'] }}
6+
@endif
7+
{!! $msg['html'] !!}
8+
@if (!empty ($msg['items']))
9+
<ul>
10+
@foreach ($msg['items'] as $item)
11+
<li>{{ $item }}</li>
12+
@endforeach
13+
</ul>
14+
@endif
15+
@if ( ! empty ($msg['buttons']))
16+
<p>
17+
@foreach ($msg['buttons'] as $btn)
18+
<a class="btn btn-{{ $btn['class'] }}" href="{{ $btn['url'] }}">{{ $btn['text'] }}</a>
19+
@endforeach
20+
</p>
21+
@endif
322
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
423
<span aria-hidden="true">&times;</span>
524
</button>
6-
<strong>{{ $msg['message'] }}</strong>
7-
8-
@if (!empty ($msg['details']))
9-
<p>{{ $msg['details'] }}</p>
10-
@endif
11-
12-
{!! $msg['html'] !!}
13-
14-
@if (!empty ($msg['items']))
15-
<ul>
16-
@foreach ($msg['items'] as $item)
17-
<li>{{ $item }}</li>
18-
@endforeach
19-
</ul>
20-
@endif
21-
22-
@if ( ! empty ($msg['buttons']))
23-
<p>
24-
@foreach ($msg['buttons'] as $btn)
25-
<a class="btn btn-{{ $btn['class'] }}" href="{{ $btn['url'] }}">{{ $btn['text'] }}</a>
26-
@endforeach
27-
</p>
28-
@endif
2925
</div>
30-
@endforeach
26+
@endforeach
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
<div id="field_{{ $id }}"{!! Html::classes(['checkbox', 'error' => $hasErrors]) !!}>
2-
<label>
3-
{!! $input !!}
1+
<div id="field_{{ $id }}" class="form-check">
2+
{!! $input !!}
3+
<label class="form-check-label" for="{{ $id }}">
44
{{ $label }}
5+
@if ($required)
6+
<span class="badge badge-info">Required</span>
7+
@endif
58
</label>
6-
7-
@if ($required)
8-
<span class="label label-info">Required</span>
9-
@endif
10-
11-
@if (!empty($errors))
12-
<div class="controls">
13-
@foreach ($errors as $error)
14-
<p class="help-block">{{ $error }}</p>
15-
@endforeach
16-
</div>
17-
@endif
9+
@foreach ($errors as $error)
10+
<div class="invalid-feedback">{{ $error }}</div>
11+
@endforeach
1812
</div>
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
<h4>{{ $label }}</h4>
2-
3-
{!! $input !!}
4-
5-
@if ( ! empty($errors))
6-
<div class="controls">
7-
@foreach ($errors as $error)
8-
<p class="help-block">{{ $error }}</p>
9-
@endforeach
10-
</div>
1+
<h4{!! Html::classes(['text-danger' => $hasErrors]) !!}>
2+
{{ $label }}
3+
@if ($required)
4+
<span class="badge badge-info">Required</span>
115
@endif
6+
</h4>
7+
{!! $input !!}
8+
@foreach ($errors as $error)
9+
<p class="text-danger">{{ $error }}</p>
10+
@endforeach
1211

13-
<hr>
12+
<br>
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
<div id="field_{{ $id }}"{!! Html::classes(['form-group', 'has-error' => $hasErrors]) !!}>
2-
<label for="{{ $id }}" class="control-label">
1+
<div id="field_{{ $id }}" class="form-group">
2+
<label for="{{ $id }}"{!! Html::classes(['text-danger' => $hasErrors]) !!}>
33
{{ $label }}
4+
@if ($required)
5+
<span class="badge badge-info">Required</span>
6+
@endif
47
</label>
5-
6-
@if ($required)
7-
<span class="label label-info">Required</span>
8-
@endif
9-
10-
<div class="controls">
11-
{!! $input !!}
12-
@foreach ($errors as $error)
13-
<p class="help-block">{{ $error }}</p>
14-
@endforeach
15-
</div>
8+
{!! $input !!}
9+
@foreach ($errors as $error)
10+
<div class="invalid-feedback">{{ $error }}</div>
11+
@endforeach
1612
</div>
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
@php
2+
$classes = 'form-check-input';
3+
if ($hasErrors) {
4+
$classes .= ' is-invalid';
5+
}
6+
@endphp
17
@foreach($checkboxes as $checkbox)
2-
<label class="checkbox-inline">
8+
<div class="form-check form-check-inline">
39
{!! Form::checkbox(
410
$checkbox['name'],
511
$checkbox['value'],
612
$checkbox['checked'],
7-
['id' => $checkbox['id']]
13+
['class' => $classes, 'id' => $checkbox['id']]
814
) !!}
9-
{{ $checkbox['label'] }}
10-
</label>
11-
@endforeach
15+
<label class="form-check-label" for="{{ $checkbox['id'] }}">
16+
{{ $checkbox['label'] }}
17+
</label>
18+
</div>
19+
@endforeach
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
@php
2+
$classes = 'form-check-input';
3+
if ($hasErrors) {
4+
$classes .= ' is-invalid';
5+
}
6+
@endphp
17
@foreach($checkboxes as $checkbox)
2-
<div class="checkbox">
3-
<label>
4-
{!! Form::checkbox(
5-
$checkbox['name'],
6-
$checkbox['value'],
7-
$checkbox['checked'],
8-
['id' => $checkbox['id']]
9-
) !!}
8+
<div class="form-check">
9+
{!! Form::checkbox(
10+
$checkbox['name'],
11+
$checkbox['value'],
12+
$checkbox['checked'],
13+
['class' => $classes, 'id' => $checkbox['id']]
14+
) !!}
15+
<label class="form-check-label" for="{{ $checkbox['id'] }}">
1016
{{ $checkbox['label'] }}
1117
</label>
1218
</div>
13-
@endforeach
19+
@endforeach

0 commit comments

Comments
 (0)