How to create forms in laravel

Rakesh Sharma      No Comments  

How to create forms in laravel 5 using blade templating :-

Form is the basic need for any of web application to enter data then collect and insert. It’s easy to create forms in laravel using blade templating. We doesn’t need to write more code to creating forms in laravel. Laravel supports more handy methods to create form and form fields. laravel provide an excellent form class to make form creation easy. we need to just call form class methods to create forms. we are using blade templating to create forms in laravel 5. so let’s start how to create forms in laravel 5 using blade templating.

Create form in laravel 5 using blade templating :-

Methods, Form opening and close :-

Default method of a form will be POST. In laravel some extra form methods included are PUT and DELETE, HTML forms accepts POST or GET methods. If you use PUT or DELETE methods, A hidden field will be add on your laravel form.
PUT :- method when you want to Update a record,
DELETE :- method when you want to Delete a record.
Just simple add to your form method array to apply any method.


{!! Form::open(array('url'=>'form-submit', 'method'=>'POST', 'accept-charset'=>'UTF-8','files'=>true)) !!}
//form contents here
{!! Form::close() !!}

parameters :-
url:- url to submit a form
method :- GET/POST/PUT/DELETE
files :- if form contain file upload input

Form open routes :-

In laravel there are more way to give a form submit request routes these are named routes and controller actions.


{!! Form::open(array('route' => 'route.name')) !!}
{!! Form::open(array('action' => 'Controller@method')) !!}

Above we have form open and form routes of topic how to create forms in laravel 5. Now are going to explore how to create forms fields in laravel. so below we will see about most of form fields using blade template.

Labels :- To create a forms field label you need to just pass fields name and text for label.


{!! Form::label('username','Username',array('id'=>'','class'=>'')) !!}

Text Inputs :- To generate text input you need to pass name of field, also you can set a default value of input field.


{!! Form::text('nameoffield','default-value',array('id'=>'','class'=>'', 'placeholder' => '')) !!}

Textarea Inputs:- To generate form textarea you need to pass name of textarea field, also you can set a default value of textarea field.


{!! Form::textarea('message','default-value',array('id'=>'','class'=>'', 'placeholder' => '')) !!}

Password Inputs :- To generate password input field just use password method of laravel form class.


{!! Form::password('password',array('id'=>'','class'=>'', 'placeholder' => '')) !!}

Email Inputs :- To generate form email input field just use email method of laravel form class.


{!! Form::email('email','default-value',array('id'=>'','class'=>'', 'placeholder' => '')) !!}

File Inputs :- if you want to upload a file you need to use file form fields Note: The form must have been opened with the files option set to true.


{!! Form::open(array('url'=>'form-submit', 'files'=>true)) !!}

Then just pass your file input name.


{!! Form::file('image') !!}

Selectboxes :- For create select box element pass a name as a first argument then pass an array of options and third parameter will be default value.


{!! Form::select('status',array('enabled'=>'Enabled','disabled'=>'Disabled'),'enabled') !!}

Radio and checkbox also need to use handy methods for creation just pass name, value to set and third parameter will be which you want to default checked.
Radio Buttons :-


{!! Form::radio('status','enabled',true) !!} Enabled
{!! Form::radio('status','disabled') !!} Disabled

CheckBoxes


{!! Form::checkbox('status','1',true) !!} Enabled

Hidden Inputs


{!! Form::hidden('myhidden','1') !!}

Buttons :- you can generate all button easily by calling handy method of laravel forms class and pass name of buttons.


{!! Form::submit('Save', array('id' => '', 'class'=>'')) !!}  
{!! Form::reset('Reset', array('id' => '', 'class'=>'')) !!}
{!! Form::button('Normal') !!}

Now we can create a simple form in laravel using blade templating.
For more know about how to create forms in laravel 5 follow Forms & Html.