Forms with Fillform
This document describes how to implement forms in your app through the fillform api.
#
Setupnote
First be sure to have setup correctly the following two variables in your .env
file:
#
Create a formEverytime you need a form start by creating a component with npx acanto component MyForm
. It will act as a wrapper of the actual form, allowing scoped props, styles and scripts modifications. You will be able to use it wherever you like in your templates with <x-MyForm />
.
Now you have two ways of implementing your form through fillform.
#
Standard implementationThis implementation requires very little code and typically manages contact forms and newsletter subscriptions. To have a standard form submitted in ajax, with client side validation and feedback messages you will neeed:
- In your template
/src/components/MyForm/index.blade.php
:
Note that here we pass to x-fillform-base
a forms
prop with an array of the localised version ids of the form we want. This array will be used internally to construct the $form
data.
- In your javascript
/src/components/MyForm/index.js
:
#
Custom implementationThere are scenarios in which you want to customize the form data coming from fillform.
note
Note that the data form fillform can come either directly or indirectly, as in the authentication forms where the data is "forwarded" from Fillform by the CMS to the Frontend.
To accomplish this you will need to use your form component class in /src/components/MyForm/index.php
and build the $form
data with the Fillform
service (namespace LaravelFrontend\Forms\Fillform
):
the get
method takes the same array of forms as you would pass to <x-fillform-base forms="..." />
and a second bool
argument indicates if the form includes a newsletter subscription management (default false
).
In your template /src/components/MyForm/index.blade.php
you use the $form
data as such:
Note that here we pass to x-fillform-base
a form
prop with an already constructed $form
data array.
note
The JavaScript side is not affected by this approach!
In your controller you can customize the $form
data as you like, here some exmples of common use cases.
#
Prefilling values with data sourceIn your component class in /src/components/MyForm/index.php
:
#
Populate select options with other sourceIn your component class in /src/components/MyForm/index.php
:
#
Sending fillform to custom frontend endpointsrc/components/MyForm/index.blade.php
:
src/components/MyForm/index.js
:
#
Sending fillform to custom CMS endpointsrc/components/MyForm/index.blade.php
:
src/components/MyForm/index.js
: