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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package uf_formgenerator

Form Generator Sprinkle for UserFrosting 5

Donate Latest Version UserFrosting Version Build PHPStan Codecov StyleCI

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 :

  1. Install FormGenerator through Composer:

  2. Add UserFrosting\Sprinkle\FormGenerator\FormGenerator to your Sprinkle Recipe sprinkle method.

  3. To use the frontend helper, first install the npm dependency:

  4. Then add this entry to your webpack.config.js, in the sprinkles list :

  5. Each template file where you want to use the frontend helper, add this line to {% block scripts_page %}:

  6. 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 :

  1. composer install
  2. php bakery bake
  3. 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 :

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:

  1. Setup your form schema (as described above)
  2. Setup the form in your controller
  3. 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:

  1. box_id: This should always be $get['box_id']. This is used by the JavaScript code to actually display the modal.
  2. box_title: The title of the modal.
  3. submit_button: The label of the submit button. Optional. Default to SUBMIT (localized).
  4. form_action: The route where the form will be sent
  5. fields: The fields. Should always be $form->generate()
  6. 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

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.

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

PHP Build Version
Package Version
Requires php Version ^8.0
userfrosting/framework Version ~5.1.0
userfrosting/sprinkle-core Version ~5.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lcharette/uf_formgenerator contains the following files

Loading the files please wait ....