Laraform
Laraform is a package for Laravel that aims to remove a tedious issue with Laravel that we all experience on a daily basis. Not only will it solve a tedious issue, it will also improve development.
Developing a strong and secure Website requires you validate all inputs from the user. Luckily for us, Laravel already does this for us through request validators. But if you ever went through multiple revisions of a website, you may have experienced the tedious task of updating your HTML input forms in a blade file then moving over to the validator request file and updating the rules and error messages. This can get very annoying quickly.
Thus the creation of Laraform.
Now instead of updating your HTML forms in your blade files and rules/custom message in the request validator files. You only need to edit a JSON file. Laraform offers a dynamic HTML form generation and request validator file generation based on a custom JSON. Now instead of going through the tedious steps, you update one file and you are done. This drastically increases productivity.
Install
Install is as simple as any other composer package. So let's get started!
First, you want to require Laraform with composer.
composer require sooonmitch/laraform:1.*
Now we want to add Laraform to the providers array in config/app.php
Sooonmitch\Laraform\LaraFormServiceProvider::class
That is it! You are ready to go.
How to use
Now that Laraform is installed, we need to use it.
The first thing we need to do is generate the json file. Run php artisan laraform:generate
to create the json file.
The json file is the base to Laraform. Without it, Laraform cannot run. Once laraformSchema.json
is generated in the resources
folder, you can go about and edit the json to what you need.
JSON Break down
Here is what the generated json file looks like:
"profileSubmit": {
"type": "post",
"formAction": "#",
"formInclude": "style='background:green;'",
"buttonInclude": "class='btn btn-default'",
"parameters": {
"name": {
"rule": "required",
"customMsg": "You have to give your name.",
"type": "text",
"include": "value='test' class='form-control'",
"beforeInclude": "<div class='form-group'><label>Name:</label>",
"afterInclude": "</div>"
},
"age": {
"rule": "required|integer",
"customMsg": "Please make sure you enter your age.",
"type": "number",
"include": "min='1' max='50' class='form-control'",
"beforeInclude": "<div class='form-group'><label>Age:</label>",
"afterInclude": "</div>"
}
}
}
Lets break down what is going on in this json file.
The profileSubmit
is the name of the form. You will be using this name to reference the form from now on. The request validator file will be generated with the name also.
Inside profileSubmit
, you will see multiple fields.
-
type
: This is the type of form you need. Post, Patch, etc -
formAction
: This is what you want theaction=""
to contain -
formInclude
: This is everything else you want to include in the form tag. I.E style, class, etc. -
buttonInclude
: This is what you want to include in the button tag -
parameters
: Parameters are the input fields for the form.-
name
: This is the name of the input field-
rule
: This is the Laravel rules that you want the input to have. This follows Laravels format. -
customMsg
: This is the Laravel custom messages you want the input to have. This follows Laravels format. -
type
: The type of input you want. -
include
: Everything you want to include in the input tag will be in this. -
beforeInclude
: Everything you want to include before the input. E.I "div tags, label tags, etc" -
afterInclude
: Everything you want to include after the input. E.I "closing div tags, closing label tags, etc"
-
-
Keep in mind that you can add as many forms and parameters in the forms as possible.
Laravel Request Validator Understanding
Everytime you add forms or parameters in the json file. Make sure you update the request validator files by running the php artisan laraform:generate
command.
That is all you ever need to do with the request validator files. You no longer have to touch these files because they will be updated automatically.
HTML use
After you setup, the json file and generate the validator request files. It is time to use the magic of Laraform in your blade files.
To automatically generate the form in your blade files, include:
{{ Sooonmitch\Laraform\LaraForm::generateForm('profileSubmit') }}
You can replace profileSubmit
with the name of the forms in your json file.
That is all you need to do with the html side. It will generate the form in html format automatically when the page is called.
Conclusion
As you see, Laraform really speeds up production and automation. As a developer, it was very tedious and annoying to change an html form in a blade file then go to the validator request file and replace/update that. Laraform was created to remove this annoying task and make development for forms a breeze.
Features
- Dynamic html form generation
- Automatic request validator generation
- Simple and easy to learn/use