Download the PHP package rafaelwendel/ci4formbuilder without Composer
On this page you can find all versions of the php package rafaelwendel/ci4formbuilder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rafaelwendel/ci4formbuilder
More information about rafaelwendel/ci4formbuilder
Files in rafaelwendel/ci4formbuilder
Package ci4formbuilder
Short Description Library to build and manage forms in a CodeIgniter 4 projects (Object-Oriented way).
License MIT
Homepage https://github.com/rafaelwendel/CI4FormBuilder
Informations about the package ci4formbuilder
CI4FormBuilder
Library to build and manage forms in a CodeIgniter 4 projects (Object-Oriented way).
Content
- Instalation & Loading
- Components
- How to use
- Basic Usage
- Using a Template
- Setting Form Data
- Setting Error Messages
- Bonus: Form Types
Instalation & Loading
CI4FormBuilder is available on Packagist, and instalation via Composer is the recommended way to install it. Add the follow line to your composer.json file:
or run
Components
The following form components are available to use.
ButtonCheckboxDropdownHiddenInputLabelMultiselectPasswordRadioResetSubmitTextareaUpload
How to use
Basic Usage
The cited components can be instantiated and added to a Form.
The above code prints the following result
Another example using more components
Output:
Using a Template
With the Template class it is possible to define a default layout for a Form and its components. The following options are available for use
beforeForm: code to insert before the<form>tagafterForm: code to insert after the</form>close tagbeforeComponent: code to insert before each componentafterComponent: code to insert after each componentbeforeErrorMessage: code to insert before a component error message (like validation)afterErrorMessage: code to insert after a component error messagebuttonExtra: extra attributes (class,id,onclick, etc.) to buttons (can use array, object or string)checkboxExtra: extra attributes to checkboxesdropdownExtra: extra attributes to dropdownshiddenExtra: extra attributes to hiddensinputExtra: extra attributes to inputslabelExtra: extra attributes to labelsmultiselectExtra: extra attributes to multiselectspasswordExtra: extra attributes to passwordsradioExtra: extra attributes to radiosresetExtra: extra attributes to resetssubmitExtra: extra attributes to submitstextareaExtra: extra attributes to textareasuploadExtra: extra attributes to uploads
Example
Output:
Setting Form Data
You can pass the value of a component in the constructor itself. Like:
However, it is also possible to pass an array or object containing the data to be set through the setFormData method in $form instance. Data can be provide from a model or a request.
Setting Error Messages
To include error messages (usually validation errors), just pass them to the setErrorsValidation method. In addition, also configure the template for displaying errors
Output
Bonus: Form Types
The FormTypeAbstract class provides a standard structure for creating types (objects) related to the form that will be created.
To make the creation of these classes easier, you can generate a FormType class using the Spark CLI command provided by this library:
For example, to create a product form type, run:
This command will automatically create the ProductType class in the app/FormTypes folder, extending FormTypeAbstract and containing the template structure.
For example, let's implement a product form with name and price fields. To do this, we will create the ProductType (extending FormTypeAbstract) class in the app/FormTypes folder (either manually or using the generator command above). (Note: we must implement the abstract setComponents method defined in the FormTypeAbstract class)
We will use this ProductType in the Products controller
Same type for edit action
Finally, in the type constructor it is possible to pass the template option (discussed in Using a Template) and the submit label.