https://github.com/misantron/bricks
Flexible form builder with data validation and pre processing
https://github.com/misantron/bricks
form form-builder form-validation framework-agnostic php
Last synced: 24 days ago
JSON representation
Flexible form builder with data validation and pre processing
- Host: GitHub
- URL: https://github.com/misantron/bricks
- Owner: misantron
- License: mit
- Created: 2015-09-18T09:39:52.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2018-09-15T12:25:19.000Z (almost 8 years ago)
- Last Synced: 2025-06-13T02:05:36.108Z (about 1 year ago)
- Topics: form, form-builder, form-validation, framework-agnostic, php
- Language: PHP
- Homepage:
- Size: 50.8 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Bricks
[](https://travis-ci.org/misantron/bricks)
[](https://scrutinizer-ci.com/g/misantron/bricks)
[](https://scrutinizer-ci.com/g/misantron/bricks)
[](https://packagist.org/packages/misantron/bricks)
[](https://github.com/misantron/bricks)
Flexible form builder with data validation and pre processing.
## Installation
Run [Composer](https://getcomposer.org) from command line:
```
composer require misantron/bricks
```
Or add dependency to `composer.json`:
```
{
"require": {
"misantron/bricks": "dev-master"
}
}
```
## Usage
Create a new form by inheriting `\Bricks\AbstractForm`. Abstract method `fields()` must be implemented with configuration array:
```php
class SomeForm extends \Bricks\AbstractForm
{
protected funtion fields(): array
{
return [
'foo' => [
'type' => 'string', // using for data type cast
'validators' => [
'required' => true,
'lengthMax' => 64,
],
],
'bar' => [
'type' => 'integer', // using for data type cast
'validators' => [
'required' => true,
'in' => function () {
return [1, 2];
}
],
'cleanup' => true, // flag that field will be deleted from getData() method call response
],
];
}
}
```
Form workflow inside application controller/service/etc.:
```php
$request = Request::fromGlobals(); // must implements \Psr\Http\Message\RequestInterface
$default = [
'foo' => 'test',
];
$form = \SomeForm::create()
->setData($default) // allows to pass an initial data before handling the request
->handleRequest($request) // get data from the request and data processing
->validate(); // data validation
$data = $form->getData(); // extracting processed and validated data from form
```
## Built-in field types
`string`, `integer`, `float`, `boolean`, `array` (contains elements of different types), `dateTime` (transform datetime string or timestamp to [Carbon](https://github.com/briannesbitt/Carbon) object) , `intArray`, `strArray`, `floatArray`
Custom user type can be easily added:
```php
class MyTransducer extends \Bricks\Data\Transducer
{
private funtion myType($value)
{
// your custom logic here
}
}
```
## Built-in field validation rules
See [Valitron](https://github.com/vlucas/valitron) documentation.
If form data is not valid - `\Bricks\Exception\ValidationException` will be thrown:
```php
try {
$form->validate();
} catch (\Bricks\Exception\ValidationException $e) {
var_dump($e->getData()); // getting fields error data
}
```