【问题标题】:Merging laravel's Form class with Bootstraps form-group class将 laravel Form 类与 Bootstrap form-group 类合并
【发布时间】:2016-09-27 02:51:21
【问题描述】:

我在 laravel 中有一个表单,可以使用 bootstrap 编辑帖子:

@section('content')
    <form method="PUT" action="{{ URL::route('post.update', array($post->id)) }}">
        <div class="form-group">
            <label for="usr">Title:</label>
            <input type="text" class="form-control" id="usr" name="title" value="{{$post->title}}">

            <br>

            <label for="comment">Details:</label>
            <textarea class="form-control" rows="5" id="comment" name="detail"
                      placeholder="Write a new post.">"{{$post->message}}"</textarea>
            <input type="hidden" name="id" value="add">
        </div>
        <a href="/" class="btn btn-danger" role="button">Cancel</a>
        <div class="pull-right">
            <input type="submit" class="btn btn-success" value="Update">
        </div>
    </form>


@stop

看起来像这样:

现在,如果我使用 Laravel 的 Form 类,它会更小更整洁:

{{Form::model($post, array('method' => 'PUT', 'route' => array('post.update', $post->id)))}}
{{ Form::label('title', 'Post Title: ') }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<p></p>
{{ Form::label('message', 'Post Message: ') }}
{{ Form::text('message') }}

{{ $errors->first('message') }}
<p></p>
{{ Form::submit('Update') }}
{{ Form::close() }}

但是,这会导致表单看起来很丑:

那么,简而言之,有没有办法使用 Laravel 的表单类并为其添加 boostrap 样式/按钮?我想要具有引导程序视觉效果的表单类的功能和易用性。

【问题讨论】:

  • 我迟到了,但我强烈推荐this 包。安装后,您应该可以将Form 替换为BootForm

标签: php forms twitter-bootstrap laravel


【解决方案1】:

您需要将类添加到每个输入(这里是一个示例

   {{Form::model($post, array('method' => 'PUT', 'route' => array('post.update', $post->id)))}}
   {!! Form::label('title','Post Title: ', array('class' => 'pull-left'))  !!}
   {!! Form::text('title', null, ['class' => 'form-control'])!!}
   {!! Form::label('message','Post Message: ', array('class' => 'pull-left'))  !!}
   {!! Form::textarea('message', null, ['class' => 'form-control'])!!}
    <br>
    <a href="/" class="btn btn-danger pull-left" role="button">Cancel</a>
   {!! Form::submit('Update',  ['class' => 'btn btn-success pull-right']) !!}
   {!! Form::close() !!}

【讨论】:

  • 谢谢,稍微改进了一下,但看起来还是不太好。
  • 现在检查,但是,我在帮助您如何向表单添加类,我不是试图给您您正在寻找的 100% 正确的表单,我向您展示如何做到这一点
  • 哦,我明白了。谢谢你。我会玩弄它并让它正确。我注意到您使用了“Form::open”。为什么要使用它而不是“Form::Model”?
  • 如果我使用 Form::Model 它会抛出异常,因为我没有模型,仅此而已,我编辑了答案,如果这回答了您的问题,请将此问题作为已回答
  • 我们可以添加多个类吗?
猜你喜欢
  • 2014-04-10
  • 2017-11-09
  • 2016-09-05
  • 2015-11-22
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 2019-04-22
  • 1970-01-01
相关资源
最近更新 更多