marvin255 / serviform
Php form constructor
Installs: 2 221
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: >=5.6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.2
- phpmetrics/phpmetrics: *
- phpunit/phpunit: ^5.0|^7.0
- sebastian/phpcpd: *
- dev-master
- v2.0.0
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.2
- 0.0.1
- dev-feature/version-3.0
This package is auto-updated.
Last update: 2022-07-29 17:30:37 UTC
README
Form constructor for php.
Installation
Via Composer
Run command in project folder:
composer require marvin255/serviform:~1.2
Common
Download library archive and extract to project. Make sure that Autoloader.php
is included:
require_once 'src/Autoloader.php';
Basic usage
Use \marvin255\serviform\helpers\FactoryFields
to create form. For each element must be specified an array with required type
key or set an object that implements \marvin255\serviform\interfaces\Field
interface. For each rule must be specified an array like shown below.
use marvin255\serviform\helpers\FactoryFields; $form = FactoryFields::initElement('form', [ 'name' => 'feedback', 'elements' => [ 'name' => [ 'label' => 'Name', 'type' => 'input', 'attributes' => [ 'class' => 'class', 'data-attribute' => 'some attribute', ], ], 'email' => [ 'label' => 'Email', 'type' => 'input', ], 'message' => [ 'label' => 'Message', 'type' => 'textarea', ], 'send' => [ 'label' => 'Send', 'type' => 'button', ], ], 'rules' => [ [['name', 'email', 'message'], 'required'], [['email'], 'regexp', 'regexp' => 'email'], ], ]);
Load data to form. Validate form fields. If all checks are passed do some action.
if ($form->loadData() && $form->validate()) { //get data form form $formData = $form->getValue(); //here is some action if form's data is valid, e.g. mail() or redirect }
Render form.
echo $form;
Advanced usage
To create stepped form or insert one form to other form set new form
element as an element of base form. You can insert any form to any other form with no nesting limit.
use marvin255\serviform\helpers\FactoryField; $form = FactoryFields::initElement('form', [ 'name' => 'feedback', 'elements' => [ 'message' => [ 'type' => 'form', 'elements' => [ 'name' => [ 'label' => 'Name', 'type' => 'input', ], 'email' => [ 'label' => 'Email', 'type' => 'input', ], 'message' => [ 'label' => 'Message', 'type' => 'textarea', ], ], 'rules' => [ [['name', 'email', 'message'], 'required'], [['email'], 'regexp', 'regexp' => 'email'], ], ], 'address' => [ 'type' => 'form', 'elements' => [ 'country' => [ 'label' => 'Country', 'type' => 'input', ], 'city' => [ 'label' => 'City', 'type' => 'input', ], 'street' => [ 'label' => 'Street', 'type' => 'input', ], ], 'rules' => [ [['country', 'city', 'street'], 'required'], ], ], 'send' => [ 'type' => 'button', 'label' => 'Send', ], ], ]); if ($form->loadData() && $form->validate()) { //get data form form $formData = $form->getValue(); //here is some action if form's data is valid, e.g. mail() or redirect } echo $form;
To create form with duplicated fields there is no need to duplicate all their descriptions. Just use multiple
field type.
use marvin255\serviform\helpers\FactoryField; $form = FactoryFields::initElement('form', [ 'name' => 'feedback', 'elements' => [ 'message' => [ 'type' => 'form', 'elements' => [ 'name' => [ 'label' => 'Name', 'type' => 'input', ], 'email' => [ 'label' => 'Email', 'type' => 'input', ], 'message' => [ 'label' => 'Message', 'type' => 'textarea', ], ], 'rules' => [ [['name', 'email', 'message'], 'required'], [['email'], 'regexp', 'regexp' => 'email'], ], ], 'address' => [ 'type' => 'multiple', 'min' => 3, 'max' => 3, 'multiplier' => [ 'type' => 'form', 'elements' => [ 'country' => [ 'label' => 'Country', 'type' => 'input', ], 'city' => [ 'label' => 'City', 'type' => 'input', ], 'street' => [ 'label' => 'Street', 'type' => 'input', ], ], 'rules' => [ [['country', 'city', 'street'], 'required'], ], ], ], 'send' => [ 'type' => 'button', 'label' => 'Send', ], ], ]); if ($form->loadData() && $form->validate()) { //get data form form $formData = $form->getValue(); //here is some action if form's data is valid, e.g. mail() or redirect } echo $form;
In that case form with address will be render three times with different name
parameters.
Fields
All fields must implement \marvin255\serviform\interfaces\Field
. To add new field type to factory or change old one use \marvin255\serviform\helpers\FactoryFields::setDescription
.
Add new field type.
use serviform\helpers\FactoryFields; FactoryFields::setDescription('new_field_type', [ 'type' => '\My\Awesome\Field', // required, string with the name of new type class that implements \marvin255\serviform\interfaces\Field 'label' => 'Default label', // we can set any default setting for each of newly created fields 'attributes' => [ 'class' => 'form-control', ], ]);
Redefine old type.
use serviform\helpers\FactoryFields; FactoryFields::setDescription('input', [ 'type' => '\My\Awesome\Input', // we can set new class for builtin field types 'label' => 'Default label', // we can set any default setting for each of newly created fields ]);
Validation rules
All validation rules must implement \marvin255\serviform\interfaces\Validator
. To add new validation rule to factory or change old one use \marvin255\serviform\helpers\FactoryValidators::setDescription
.
Add new rule.
use serviform\helpers\FactoryValidators; FactoryValidators::setDescription('new_rule', [ 'type' => '\My\Awesome\Rule', // required, string with the name of new rule class that implements \marvin255\serviform\interfaces\Validator 'skipOnError' => true, // we can set any default setting for each of newly created rule ]);
Redefine old rule.
use serviform\helpers\FactoryValidators; FactoryFields::setDescription('require', [ 'type' => '\My\Awesome\Require', // we can set new class for builtin rule 'skipOnError' => true, // we can set any default setting for each of newly created rules ]);