Bricks
, (*1)
Flexible form builder with data validation and pre processing., (*2)
Installation
Run Composer from command line:, (*3)
composer require misantron/bricks
Or add dependency to composer.json:, (*4)
{
"require": {
"misantron/bricks": "dev-master"
}
}
Usage
Create a new form by inheriting \Bricks\AbstractForm. Abstract method fields() must be implemented with configuration array:, (*5)
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.:, (*6)
$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 object) , intArray, strArray, floatArray, (*7)
Custom user type can be easily added:, (*8)
class MyTransducer extends \Bricks\Data\Transducer
{
private funtion myType($value)
{
// your custom logic here
}
}
Built-in field validation rules
See Valitron documentation.
If form data is not valid - \Bricks\Exception\ValidationException will be thrown:, (*9)
try {
$form->validate();
} catch (\Bricks\Exception\ValidationException $e) {
var_dump($e->getData()); // getting fields error data
}