Skip to content

Commit 54b1af7

Browse files
Add form macro for label to allow custom for and id attributes.
1 parent 96be0a7 commit 54b1af7

File tree

13 files changed

+32
-23
lines changed

13 files changed

+32
-23
lines changed

src/Kris/LaravelFormBuilder/Fields/FormField.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ protected function prepareOptions(array $options = [])
275275
$this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper')));
276276
$this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors')));
277277

278-
if ($this->getOption('is_child')) {
279-
$this->setOption('labelAttrs', $helper->prepareAttributes($this->getOption('label_attr')));
280-
}
281-
282278
if ($this->getOption('help_block.text')) {
283279
$this->setOption(
284280
'help_block.helpBlockAttrs',

src/Kris/LaravelFormBuilder/Fields/StaticType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public function render(array $options = [], $showLabel = true, $showField = true
1414
private function setupStaticOptions(&$options)
1515
{
1616
$options['elemAttrs'] = $this->formHelper->prepareAttributes($this->getOption('attr'));
17-
$options['labelAttrs'] = $this->formHelper->prepareAttributes($this->getOption('label_attr'));
1817
}
1918

2019
/**

src/Kris/LaravelFormBuilder/FormBuilderServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ public function boot()
5656
__DIR__ . '/../../views' => base_path('resources/views/vendor/laravel-form-builder'),
5757
__DIR__ . '/../../config/config.php' => config_path('laravel-form-builder.php')
5858
]);
59+
60+
$form = $this->app['form'];
61+
62+
$form->macro('customLabel', function($name, $value, $options = []) use ($form) {
63+
if (isset($options['for']) && $for = $options['for']) {
64+
unset($options['for']);
65+
return $form->label($for, $value, $options);
66+
}
67+
68+
return $form->label($name, $value, $options);
69+
});
5970
}
6071

6172
/**

src/views/checkbox.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
<?php endif; ?>
1212

1313
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
14-
<?php if ($options['is_child']): ?>
15-
<label <?= $options['labelAttrs'] ?>><?= $options['label'] ?></label>
16-
<?php else: ?>
17-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
18-
<?php endif; ?>
14+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
1915
<?php endif; ?>
2016

2117
<?php include 'errors.php' ?>

src/views/child_form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<?php endif; ?>
66

77
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
8-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
8+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
99
<?php endif; ?>
1010

1111
<?php if ($showField): ?>

src/views/choice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<?php endif; ?>
66

77
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
8-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
8+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
99
<?php endif; ?>
1010

1111
<?php if ($showField): ?>

src/views/collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<?php endif; ?>
66

77
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
8-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
8+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
99
<?php endif; ?>
1010

1111
<?php if ($showField): ?>

src/views/radio.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
<?php endif; ?>
1212

1313
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
14-
<?php if ($options['is_child']): ?>
15-
<label <?= $options['labelAttrs'] ?>><?= $options['label'] ?></label>
16-
<?php else: ?>
17-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
18-
<?php endif; ?>
14+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
1915
<?php endif; ?>
2016

2117
<?php include 'errors.php' ?>

src/views/select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<?php endif; ?>
66

77
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
8-
<?= Form::label($name, $options['label'], $options['label_attr']) ?>
8+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
99
<?php endif; ?>
1010

1111
<?php if ($showField): ?>

src/views/static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<?php endif; ?>
66

77
<?php if ($showLabel && $options['label'] !== false && $options['label_show']): ?>
8-
<label <?= $options['labelAttrs'] ?>><?= $options['label'] ?></label>
8+
<?= Form::customLabel($name, $options['label'], $options['label_attr']) ?>
99
<?php endif; ?>
1010

1111
<?php if ($showField): ?>

0 commit comments

Comments
 (0)