Introduction to Laravel Form Builder
Laravel Form Builder helps you to design forms for your projects on Laravel when you want to generate an application or create anything for the web. It helps you to design your website or application and embed the forms into it within minutes. It includes the CMS and Admin modules inside it and makes it easier to generate forms wherever the user may need it. It has readily available form designs that can be directly accessed and will not create any complication. It helps you to build forms in the easiest way possible for the website or the application the user creates.
What is Laravel Form Builder?
The package that is available in Laravel as a form builder is actually presented by Kristijan Husak and it also incorporates an API inside it that is like that of Symfony’s form builder. Symfony’s form builder has been an inspiration for the creation of Laravel form builder. This Form Builder can easily run as a standalone installation as it is self-contained with all the necessary requirements to run as a complete package. You don’t need to have builder codes to run the Laravel form builder as it operates easily without the need for coding. There are readily available templates that can be incorporated into the form that the user wishes to create for the application or the webpage in the Laravel PHP Framework. Similarly, to note is the fact that Laravel does not entertain applications that are created in other PHP frameworks and brought into Laravel for editing. The essential need is that the application needs to be created inside the framework of Laravel.
In the above image, we can see a form that is created with the help of Laravel Form Builder. In the GitHub repository, there are developers who have put up their projects that were created with the help of the options made available by Laravel for form building with the help of the Laravel Form Builder.
The above is the repository image from GitHub of the Laravel Form Builder.
Among the many features available in Laravel Form Builder are a few important ones as follows:
- It allows for quick creation of forms
- It easily integrates with the Laravel Request Validation leading to efficient usage and also reusability.
- There are customizable placeholders made available for the elements inside the builder.
- It provides a very attractive set of designs made available for the form creations in the form of a set of the library.
The projects generated with the help of Laravel Form Builder to build forms have been more efficient compared to other form builders that are available in the world of PHP frameworks.
In the above image, we can notice the Laravel Form Builder 2.0 with a description of the UI, Issues, and compatibility details. We can understand a clear difference between using HTML coding which is painful against Form Builder which is easy to handle.
HTML Code:
@foreach($roles as $role)
<div class="radio">
<label>
<input type="radio" name="role" value="{{ $role->id }}"
{{ old('role', $user->role_id) == $role->id ? 'checked' : '' }}
{{ is_null($user->role_id) && $role->id == 1 ? 'checked' : '' }}
>
{{ $role->name }}
</label>
</div>
@endforeach
Form Builder:
@foreach($roles as $role)
<div class="radio">
<label>
{{ Form::radio('role', $role->id, 1) }}
{{ $role->name }}
</label>
</div>
@endforeach
Given above is the clear difference between HTML coding for form building and using Form Builder made available by Laravel. The difference in the efforts to code is noticeable. Form Builder comes out as an easy to use the builder for the forms in the application or the websites.
How does Laravel Form Builder Work?
You can get started by creating the form builder the way you want it for your project. You can generate the type of forms that you would need to keep inside your project and start working on it. Various types of forms can be incorporated inside the Laravel design framework while wanting to formulate forms for the editing purpose.
This image is of a template for a form builder that can be used to create a typical form type according to the need. You can generate the code as per the need and it will help to create the form as you would insert the code.
Once the project is downloaded from GitHub or any other source for Laravel, you can clone it and install the dependencies as they will be required for the running. Install the package that you have with you with the help of the composer that is available in Laravel. After that, open the link for the form builder that you will be able to see on a browser page and start building form by putting the necessary codes as needed. Later on, the testing of the project can be done to confirm if the form is built properly without any errors in it.
Example
A simple form can be generated with the help of this code as follows:
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
classSongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
Form objects can be then created inside the controller.
$form = $formBuilder->create(\App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
The following is then used for rendering the form object:
{!! form($form) !!}
Validation process:
$form = $formBuilder->create(\App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// ...
Available artisan command for generating forms and fields:
php artisan make:form Forms/SongForm \
--fields="name:text, lyrics:textarea, publish:checkbox"
We shall look at the code created for the following form Image:
The code for the above image is as follows:
Recommended Articles
We hope that this EDUCBA information on “Laravel Form Builder” was beneficial to you. You can view EDUCBA’s recommended articles for more information.