标签内的收音机和复选框与Laravel

时间:2014-06-23 08:10:28

标签: html twitter-bootstrap laravel

有没有办法使用Form::label复选框和无线电输入来生成bootstrap建议的标记?目的是在标签内部输入字段,而不是用id s附加它们。我想避免在Form::radio

周围硬编码标记
<div class="checkbox">
  <label>
    <input type="checkbox" value="">
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>

<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="radio">
  <label>
    <input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
    Option two can be something else and selecting it will deselect option one
  </label>
</div>

2 个答案:

答案 0 :(得分:3)

感谢Saint Genius的回答,这使我朝着正确的方向前进。 1

然而,他缺少几个分号,在复选框字段中没有包含name属性,并且在几行上分解了$ html字符串,使其难以阅读。

我稍微改写了一下:

Form::macro('labelCheckbox', function($name, $value, $label)
{
    $html = "<label>
                <input name='$name' type='checkbox' value='$value' /> $label
            </label>";

    return $html;
});

然后在视图中:

{{ Form::labelCheckbox('fruits', 'apple', 'Apple') }}

答案 1 :(得分:2)

您可以编写自己的表单宏,并使用它来构建您喜欢的html。

http://laravel.com/docs/4.2/html#custom-macros

类似的东西:

Form::macro('bootCheckbox', function($value, $label)
{
 $html = '<div class="checkbox"><label><input type="checkbox" value="';
 $html .= $value;
 $html .= '">';
 $html .= $label;
 $html .= '</label></div>'
 return $html
}

然后你可以在你的视图中这样称呼它:

{{ Form::bootCheck('i_dont_know_what_value_youd_like', 'Option one is this and that&mdash;be sure to include why it's great') }}

当然,您可以将您喜欢的任何值传递给宏并构建您想要的任何内容(假设您在宏观方面的代码是合理的)。 上面的示例应该在您显示时输出一个复选框,但它不是太干净,我还没有测试过。

关于放置代码的位置,只要链接它就可以放在任何地方。我的表单宏(有点更广泛)存在于/app/start/form_macros.php中,并且只需将require dirname(__FILE__) . '/form_macros.php';添加到结尾即可在/app/start/global.php中使用。