Skip to content

Commit 0a90317

Browse files
committed
Backport from origin: label
1 parent d7b2fc4 commit 0a90317

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,29 @@ BootForm::button('Activate', [ 'data-trigger' => 'foo' ]);
220220
BootForm::close();
221221
```
222222

223+
### Labels
224+
225+
#### Hide Labels
226+
227+
You may hide an element's label by setting the the value to `false`.
228+
229+
```php
230+
// An input with no label.
231+
BootForm::text('username', false);
232+
```
233+
234+
#### Labels with HTML
235+
236+
To include HTML code inside a label:
237+
238+
```php
239+
// A label with HTML code using array notation
240+
BootForm::text('username', ['html' => 'Username <span class="required">*</span>']);
241+
242+
// A label with HTML code using HtmlString object
243+
BootForm::text('username', new Illuminate\Support\HtmlString('Username <span class="required">*</span>'));
244+
```
245+
223246
### Help Text
224247

225248
You may pass a `help_text` option to any field to have [Bootstrap Help Text](https://getbootstrap.com/css/#forms-help-text) appended to the rendered form group.

src/BootstrapForm.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Collective\Html\HtmlBuilder;
77
use Illuminate\Contracts\Config\Repository as Config;
88
use Illuminate\Support\Facades\Lang;
9+
use Illuminate\Support\HtmlString;
910
use Illuminate\Support\Str;
1011

1112
class BootstrapForm
@@ -60,6 +61,13 @@ class BootstrapForm
6061
*/
6162
protected $rightColumnClass;
6263

64+
/**
65+
* The errorbag that is used for validation (multiple forms)
66+
*
67+
* @var string
68+
*/
69+
protected $errorBag = 'default';
70+
6371
/**
6472
* Suffix to add to the label string when a field is required
6573
*
@@ -142,6 +150,10 @@ public function open(array $options = [])
142150
return $this->model($options);
143151
}
144152

153+
if (array_key_exists('errorbag', $options)) {
154+
$this->setErrorBag($options['errorbag']);
155+
}
156+
145157
return $this->form->open($options);
146158
}
147159

@@ -610,8 +622,17 @@ public function radios(
610622
public function label($name, $value = null, array $options = [])
611623
{
612624
$options = $this->getLabelOptions($options);
625+
$escapeHtml = false;
626+
627+
if (is_array($value) and isset($value['html'])) {
628+
$value = $value['html'];
629+
} elseif ($value instanceof HtmlString) {
630+
$value = $value->toHtml();
631+
} else {
632+
$escapeHtml = true;
633+
}
613634

614-
return $this->form->label($name, $value, $options);
635+
return $this->form->label($name, $value, $options, $escapeHtml);
615636
}
616637

617638

@@ -766,11 +787,15 @@ public function select($name, $label = null, $list = [], $selected = null, array
766787
*/
767788
protected function getLabelTitle($label, $name, $options)
768789
{
790+
if ($label === false) {
791+
return null;
792+
}
793+
769794
if (is_null($label) && Lang::has("forms.{$name}")) {
770795
return Lang::get("forms.{$name}");
771796
}
772797

773-
$label = $label ?: Str::title($name);
798+
$label = $label ?: str_replace('_', ' ', Str::title($name));
774799

775800
if (isset($options['required'])) {
776801
$label = sprintf('%s %s', $label, $this->getLabelRequiredMark());
@@ -1049,6 +1074,18 @@ public function setGroupRequiredClass($cssClass)
10491074
}
10501075

10511076

1077+
/**
1078+
* Set the errorBag used for validation
1079+
*
1080+
* @param $errorBag
1081+
*
1082+
* @return void
1083+
*/
1084+
protected function setErrorBag($errorBag)
1085+
{
1086+
$this->errorBag = $errorBag;
1087+
}
1088+
10521089
/**
10531090
* Flatten arrayed field names to work with the validator, including removing "[]",
10541091
* and converting nested arrays like "foo[bar][baz]" to "foo.bar.baz".
@@ -1096,10 +1133,10 @@ protected function getFieldError($field, $format = '<span class="help-block">:me
10961133
$allErrors = $this->config->get('bootstrap_form.show_all_errors');
10971134

10981135
if ($allErrors) {
1099-
return implode('', $this->getErrors()->get($field, $format));
1136+
return implode('', $this->getErrors()->{$this->errorBag}->get($field, $format));
11001137
}
11011138

1102-
return $this->getErrors()->first($field, $format);
1139+
return $this->getErrors()->{$this->errorBag}->first($field, $format);
11031140
}
11041141
}
11051142

0 commit comments

Comments
 (0)