14 July 2016

Easy Bootstrap Forms In Laravel

The Goal

When working with Bootstrap, you are required to wrap your label and input elements in a div with a class of form-group. On top of that, all your input elements require a form-control class to be applied. Then, when you want to show errors in your forms, you have to check if the form field has any errors, add a class to the form-group if it does and finally add the error to the end of the form-group. Your forms may look something like this:
{{ Form::open([ 'route' => 'posts.store' ]) }}

    <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
        {{ Form::label('title', 'Title') }}
        {{ Form::text('title', null, ['class' => 'form-control']) }}
        {{ $errors->first('title', '<p class="help-block">:message</p>') }}
    </div>

    <div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
        {{ Form::label('status', 'Status') }}
        {{ Form::select('status', $statusOptions, null, ['class' => 'form-control']) }}
        {{ $errors->first('status', '<p class="help-block">:message</p>') }}
    </div>

{{ Form::close() }}

From http://blog.stidges.com/post/easy-bootstrap-forms-in-laravel


No comments:

Post a Comment