--- title: How To Build Forms With Laravel date: 2014-03-11 00:00:00 featured_image: https://images.unsplash.com/photo-1511533910568-be3ffdc229bb?q=90&fm=jpg&w=1000&fit=max excerpt: In fact, Laravel supports a bunch of handy methods that can handle form creation with ease. We are going to explore these methods. --- ![](https://images.unsplash.com/photo-1511533910568-be3ffdc229bb?q=90&fm=jpg&w=1000&fit=max) In fact, Laravel supports a bunch of handy methods that can handle form creation with ease. We are going to explore these methods. Before creating form fields, We need to create two routes. One for our form view and another for form submittion and validation. ```php Route::get('form', function(){ //render app/views/form.blade.php return View::make('form'); }); Route::post('form-submit', array('before'=>'csrf',function(){ //form validation come here })); ``` Then let's create our form, Just create new file `app/views/form.blade.php` and write the following. ```html Laravel {{ Form::open(array('url'=>'form-submit')) }} {{ Form::close() }} ``` Well. Let's explore resulting source file. ```html Laravel
``` Perhaps you don't like laravel defaults so let's provide our options. ```html {{ Form::open(array( 'url'=>'form-submit', 'method'=>'POST', 'accept-charset'=>'UTF-8', 'files'=>true )) }} ``` Now we are ready to submit fields to our form. ### Text Inputs Text inputs used to collect string data, Here's how to create text inputs. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('username','Username',array('id'=>'','class'=>'')) }} {{ Form::text('username','clivern',array('id'=>'','class'=>'')) }} {{ Form::close() }} ``` ### Textarea Inputs Similar to text inputs except that we will use `textarea()` method. Here's an example. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('biog','Biog.',array('id'=>'','class'=>'')) }} {{ Form::textarea('biog','biog here',array('id'=>'','class'=>'')) }} {{ Form::close() }} ``` ### Password Inputs You know, Password inputs used to hide data and here's how to create them. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('password','Password',array('id'=>'','class'=>'')) }} {{ Form::password('password','',array('id'=>'','class'=>'')) }} {{ Form::close() }} ``` ### Email Inputs Actually it's similar to text inputs except that modern browser (typically which support HTML5) will validate user inputs. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('email','Email',array('id'=>'','class'=>'')) }} {{ Form::email('email','hello@clivern.com',array('id'=>'','class'=>'')) }} {{ Form::close() }} ``` ### Selectboxes We can use `select()` method to create selectboxes. This method take optional parameter which is the key of the value that appear as selected. Here's an example. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::select('status',array('enabled'=>'Enabled','disabled'=>'Disabled'),'enabled') }} {{ Form::close() }} ``` ### Radio Buttons Radio buttons can be created with `radio()` method like that. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::radio('status','enabled',true) }} Enabled {{ Form::radio('status','disabled') }} Disabled {{ Form::close() }} ``` ### CheckBoxes Checkboxes can be created with `checkbox()` method like that. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::checkbox('status','1',true) }} Enabled {{ Form::close() }} ``` ### Hidden Inputs Sometimes we need to submit extra data with our forms. Hidden inputs are perfect for this mission. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::hidden('record_to_update','1') }} {{ Form::close() }} ``` ### Buttons Let's take a look at the buttons that laravel provides. ```html {{ Form::open(array('url'=>'form-submit')) }} {{ Form::submit('Save') }} {{ Form::reset('Reset') }} {{ Form::button('Normal') }} {{ Form::close() }} ``` Well. I summarized all inputs that we created through article in the following snippet and then source file result. ```html Laravel {{ Form::open(array('url'=>'form-submit')) }} {{ Form::label('username','Username',array('id'=>'','class'=>'')) }} {{ Form::text('username','clivern',array('id'=>'','class'=>'')) }} {{ Form::label('biog','Biog.',array('id'=>'','class'=>'')) }} {{ Form::textarea('biog','biog here',array('id'=>'','class'=>'')) }} {{ Form::label('password','Password',array('id'=>'','class'=>'')) }} {{ Form::password('password','',array('id'=>'','class'=>'')) }} {{ Form::label('email','Email',array('id'=>'','class'=>'')) }} {{ Form::email('email','hello@clivern.com',array('id'=>'','class'=>'')) }} {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::select('status',array('enabled'=>'Enabled','disabled'=>'Disabled'),'enabled') }} {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::radio('status','enabled',true) }} Enabled {{ Form::radio('status','disabled') }} Disabled {{ Form::label('status','Status',array('id'=>'','class'=>'')) }} {{ Form::checkbox('status','1',true) }} Enabled {{ Form::hidden('record_to_update','1') }} {{ Form::submit('Save') }} {{ Form::reset('Reset') }} {{ Form::button('Normal') }} {{ Form::close() }} ``` ```html Laravel
Enabled Disabled Enabled
```