Download the PHP package rs/form-laravel without Composer
On this page you can find all versions of the php package rs/form-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package form-laravel
Laravel Form
Installation
View files can be changed by publishing them. By default they use bootstrap 4 classes.
Quick start
Creating formlets
Formlets can be created using artisan.
By default the formlet will be created in the path app/Http/Formlets
.
Add a prefix to your formlet
This is useful when you have more than one form on a page which might be sharing the same field names. In order for errors and fields to be populated correctly we can give our form a prefix.
All fields and child formlet fields will now be prefixed with the provided prefix. Also, errors will now also be added to a separate error bag of the same name.
When testing, be sure to add the prefixes to your posted values.
Add fields to formlet
Next step is to add fields to the form. Fields are added using the add method in the prepare function. Prepare allows us to setup all the fields ready for rendering.
The add function excepts any type of field which extends RS\Form\Fields\AbstractField
Notice we also added a validation rule. The rules can be defined as per the laravel documentation.
View the form
We can instantiate the form in a controller and pass the form data to a view.
The create method is creating our form view for us. We are passing a route by name so it knows where to post to and which HTTP method to use. The build will return an array which contains information about the form and the fields which can be used to render the view. We are also handling the post using the store method on the form. At the moment it is not doing much but we can see how to the handle the post later.
Rendering the view
We are using a couple of custom blade directives to render our form. The form directive requires the form key which has been passed to the view. The formlet directive will render the fields.
Handling the post
In the controller are using the store method which runs all validations before running the persist method.
If the post fails validation the form will redirect back and populate the session with errors from the form. By default all errors will be rendered next to the field which has the error.
If validation passes then by default the persist method will try and insert into the fields into a model if one has been set.
We can overwrite the persist method if required.
Validation
Hooks
Before each form is validated we can manipulate the input for the formlet before it is validated using the prepareForValidation method. This will run for each formlet.