Download the PHP package heimrichhannot/contao-form-type-bundle without Composer
On this page you can find all versions of the php package heimrichhannot/contao-form-type-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download heimrichhannot/contao-form-type-bundle
More information about heimrichhannot/contao-form-type-bundle
Files in heimrichhannot/contao-form-type-bundle
Package contao-form-type-bundle
Short Description Add types to contao form generator.
License LGPL-3.0-or-later
Homepage https://github.com/heimrichhannot/contao-form-type-bundle
Informations about the package contao-form-type-bundle
Form Type Bundle
Form type bundle is an extension for the Contao CMS to provide a generic way to create forms for specific approaches with contao form generator. It is aimed at developers and contains no build-in form types.
Feature
- Generic way to create forms for specific approaches with contao form generator
- Options event for select, checkbox and radio form fields
- Form types can support a first time wizard to setup basic form fields
Installation
Install the bundle via composer and update your database afterwards:
Usage
Create a new form type
Create a new class that extends AbstractFormType
. Register it as service with autoconfiguration enabled.
A label can be set within $GLOBALS['TL_LANG']['tl_form']['FORMTYPE']
:
First time wizard
You can add a first time wizard to your form type by implementing getDefaultFields()
method.
It expects field definitions in array format as return values.
These fields will be created when executing the wizard.
Form events
You can register event listeners for events during form livecycle. These events are mappings of the corresponding contao form hooks but will only fire for the specific form type.
Following events are available:
Event | Name |
---|---|
CompileFormFieldsEvent | huh.form_type.[TYPE].compile_form_fields |
LoadFormFieldEvent | huh.form_type.[TYPE].load_form_field |
PrepareFormDataEvent | huh.form_type.[TYPE].prepare_form_data |
ProcessFormDataEvent | huh.form_type.[TYPE].process_form_data |
StoreFormDataEvent | huh.form_type.[TYPE].store_form_data |
ValidateFormFieldEvent | huh.form_type.[TYPE].validate_form_field |
Options event
If you want to provide options for a select, checkbox or radio form field, you can register an event listener.
The event name is huh.form_type.<formtype>.<field>.options'
.
Unified Dispatcher for Field Options
Use FieldOptionsDispatcherTrait
to dispatch FieldOptionsEvent
huh.form_type.<formtype>.<field>.options
and Contao fields.<field>.options
callbacks alike.
Example:
Form Context
Implementing a form context evaluator on your from type allows you to change the form's behavior depending on the context it is used in. This can be used e.g. to load existing data into the form fields, perhaps to create an editing form.
Built-in Form Edit Context
A basic default form context evaluator providing editing functionality is built into the bundle.
Just override DEFAULT_FORM_CONTEXT_TABLE
in your form type to allow editing the respective database model:
- Create your form in Contao.
- Name your form fields like the database fields you want to edit, e.g.
title
fortl_my_table.title
. - Append the query parameter
?edit=<id>
to the url of the page your form is included in to edit the existing row with a primary key of<id>
.
If no DEFAULT_FORM_CONTEXT_TABLE
is set, the form will be treated as a creation form by default.
Create your own Form Context
To implement your own form context evaluator, override evaluateFormContext()
in your form type:
The example shows the implementation of a form context evaluator that allows editing a database model with a primary key given by the query parameter edit
.
To retrieve the current form context, call the getter on your form type:
We do not recommend to call $this->evaluateFormContext()
directly, as it will skip future cache implementations if you do.
Form Context Actions
Most commonly used form context actions are already implemented.
FormContextAction::INVALID
can be used to indicate that the form should not be processed.
To instantiate a FormContext
object with a built-in action, use the static factory methods:
Alternatively, you may also use the FormContext
constructor:
However, you may also specify your own actions when constructing a FormContext
object, or by overriding the action of an existing object.
This way you are not bound to the built-in actions.
All versions of contao-form-type-bundle with dependencies
contao/core-bundle Version ^4.13 || ^5.0.8
symfony/event-dispatcher-contracts Version ^1.0 || ^2.0 || ^3.0
symfony/translation-contracts Version ^1.0 || ^2.0 || ^3.0