Download the PHP package lcharette/uf_formgenerator without Composer
On this page you can find all versions of the php package lcharette/uf_formgenerator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lcharette/uf_formgenerator
More information about lcharette/uf_formgenerator
Files in lcharette/uf_formgenerator
Package uf_formgenerator
Short Description Form generator for UserFrosting V5
License MIT
Homepage https://github.com/lcharette/UF_FormGenerator
Informations about the package uf_formgenerator
Form Generator Sprinkle for UserFrosting 5
This Sprinkle provides helper classes, Twig template and JavaScript plugins to generate HTML forms, modals and confirm modal bases on UserFrosting validation schemas.
Help and Contributing
If you need help using this sprinkle or found any bug, feels free to open an issue or submit a pull request. You can also find me on the UserFrosting Chat most of the time for direct support.
Versions and UserFrosting support
UserFrosting Version | FormGenerator Version |
---|---|
5.1.x | 5.1.x |
5.0.x | 5.0.x |
4.4.x | 4.x.x |
4.3.x | 3.0.x & 4.0.x |
4.2.x | 3.0.x |
4.1.x | 2.0.x |
4.0.x | No Support |
Installation
To install FormGenerator in your sprinkle :
-
Install FormGenerator through Composer:
-
Add
UserFrosting\Sprinkle\FormGenerator\FormGenerator
to your Sprinkle Recipe sprinkle method. -
To use the frontend helper, first install the npm dependency:
-
Then add this entry to your
webpack.config.js
, in thesprinkles
list : -
Each template file where you want to use the frontend helper, add this line to
{% block scripts_page %}
: - Run
php bakery bake
to finish installation of the sprinkle.
Alternatively, you can add @lcharette/formgenerator/app/assets/js/widget-formGenerator.js
globally in your app main js file.
Working example
The public/
directory serves as an example of FormGenerator. You can clone this repository and install as any UserFrosting 5 sprinkle :
composer install
php bakery bake
php -S localhost:8080 -t public
This demo is not linked to any database tables, so changes are not actually saved ;)
Features and usage
Before starting with FormGenerator, you should read the main UserFrosting guide to familiarize yourself with validation schemas: (https://learn.userfrosting.com/routes-and-controllers/client-input/validation).
Form generation
Defining the fields in the schema
This sprinkle uses the schemas
used by UserFrosting to validate form data to build form. To achieve this, a new form
key is simply added to the fields found in a schema
file.
For example, here's a simple schema
used to validate a form used to create a project
. The form will contain a name
, description
and status
fields.
Note: FormGenerator works with json and YAML schemas.
At this point, with typical UserFrosting setup, you would be going into your controller and Twig files to manually create your HTML form. This can be easy if you have a two or three fields, but can be a pain with a dozen fields and more. This is where FormGenerator steps in with the use of a new form
attribute. Let's add it to our project
form :
Let's look closer at the name
field :
Here you can see that we define the type
, label
, icon
and placeholder
value for this name
field. You can define any standard form attributes, plus the icon
, label
and default
attributes. data-*
attributes can also be defined in your schema if you need them. For the select
element, a special options
attribute containing an array of key : value
can be used to define the dropdown options. The select options (as any other attributes) can also be set in PHP (see further below).
And of course, the values of the label
and placeholder
attributes can be defined using translation keys.
Currently, FormGenerator supports the following form elements :
- text (and any input supported by the HTML5 standard : number, tel, password, etc.)
- textarea
- select
- checkbox
- hidden
- alert (Display a static alert box in the form)
The controller part
Once your fields defined in the schema
json or yaml file, you need to load that schema in your controller.
First thing to do is add FormGenerator's Form
class to your "use" list :
use UserFrosting\Sprinkle\FormGenerator\Form;
Next, where you load the schema and setup the validator
, you simply add the new Form creation:
In this example, $project
can contain the default (or current value) of the fields. A data collection fetched from the database with eloquent can also be passed directly. That second argument can also be omitted to create an empty form.
Last thing to do is send the fields to Twig. In the list of returned variables to the template, add the fields
variable:
The Twig template part
Now it's time to display the form in myPage.html.twig
!
That's it! No need to list all the field manually. The ones defined in the fields
variable will be displayed by FormGenerator/FormGenerator.html.twig
. Note that this will only load the fields, not the form itself. The <form>
tag and submit
button needs to be added manually.
Modal form
What if you want to show a form in a modal window? Well, FormGenerator makes it even easier! It's basically three steps:
- Setup your form schema (as described above)
- Setup the form in your controller
- Call the modal from your template
Setup the form in your controller
With your schema in hand, it's time to create a controller and route to load your modal. The controller code will be like any basic UserFrosting modal, plus the $form
part above and one changes in the render
part. For example :
As you can see, instead of rendering your own Twig template, you simply have to specify FormGenerator's modal template. This template requires the following variables:
box_id
: This should always be$get['box_id']
. This is used by the JavaScript code to actually display the modal.box_title
: The title of the modal.submit_button
: The label of the submit button. Optional. Default toSUBMIT
(localized).form_action
: The route where the form will be sentfields
: The fields. Should always be$form->generate()
validators
: Client side validators
Call the modal from your template
So at this point you have a controller that displays the modal at a /path/to/controller
route. Time to show that modal. Again, two steps:
First, define a link or a button that will call the modal when clicked. For example :
The important part here is the data-formUrl
attribute. This is the route that will load your form. js-displayForm
is used here to bind the button to the action.
Second, load the FormGenerator JavaScript widget. Add this to your Twig file:
By default, the formGenerator
plugin will bind a form modal to every element with the js-displayForm
class.
Modal confirmation
One side features of FormGenerator is the ability to add a confirmation modal to your pages with simple HTML5 attributes. The process is similar to adding a modal form, without the need to create any controller or route.
Let's look at a delete button / confirmation for our project
:
(Note that content of data attributes can be translation keys)
If not already done, make sure the FormGenerator assets are included in your template.
By default, the formGenerator
plugin will bind a confirmation modal to every element with the js-displayConfirm
class.
Advance usage
Defining attributes in PHP
setInputArgument
Form field input attributes can also be added or edited from PHP. This can be useful when dynamically defining a Select input options. To do this, simply use the setInputArgument($inputName, $property, $data)
method. For example, to add a list to a clients
select :
setData
If you want to set the form values once the form instance is created, you can use the setData($data)
method:
setValue
Similar to the setData
method, you can set a specific input value using the setValue($inputName, $value)
method :
setFormNamespace
When dealing with multiple form on the same page or a dynamic number of input (you can use the new Loader
system in 4.1 to build dynamic schemas!), it can be useful to wrap form elements in an array using the setFormNamespace($namespace)
method. This can also your the input names to contains dot syntaxt.
For example, $form->setFormNamespace("data");
will transform all the input names from <input name="foo" [...] />
to <input name="data[foo]" [...] />
.
registerType
If you want to overwrite or add a new element type,
First, you need to create the element itself. This class needs to extends the UserFrosting\Sprinkle\FormGenerator\Element\Input
class. In there you can define the default attributes, and do other transformation. For example, to define a new Date
element type :
Next, you need to register your Date
element type. If the date
type is already registered, it will be overwritten by your custom class.
Javascript Plugin
By default, the formGenerator
plugin will bind a form modal to every element with the js-displayForm
class and will bind a confirmation modal to every element with the js-displayConfirm
class. You can
Options
The following options are available:
Just pass an object with those
mainAlertElement
(jQuery element). The element on the main page where the main alerts will be displayed. Default to$('#alerts-page')
.redirectAfterSuccess
(bool). If set to true, the page will reload when the form submission or confirmation is successful. Default totrue
.
Example:
Events
You can listen for some events returned by FormGenerator. Those events can be used to apply some actions when the modal is displayed or the form is successfully sent. For example, this is can be used with redirectAfterSuccess
on false
to refresh the data on the page when the form is submitted successfully.
formSuccess.formGenerator
displayForm.formGenerator
displayConfirmation.formGenerator
confirmSuccess.formGenerator
error.formGenerator
Example:
Running tests
FormGenerator comes with some unit tests. Before submitting a new Pull Request, you need to make sure all tests are a go. With the sprinkle added to your UserFrosting installation, simply execute the php bakery test FormGenerator
command to run the tests.
License
By Louis Charette. Copyright (c) 2020, free to use in personal and commercial software as per the MIT license.
All versions of uf_formgenerator with dependencies
userfrosting/framework Version ~5.1.0
userfrosting/sprinkle-core Version ~5.1.0