berlioz/form

v0.2.1 2019-07-16 08:26 UTC

README

Berlioz Form is a PHP library to manage your forms.

Installation

Composer

You can install Berlioz Form with Composer, it's the recommended installation.

$ composer require berlioz/form

Dependencies

  • PHP >= 7.1
  • Packages:
    • berlioz/utils
    • psr/http-message

Description

3 types of elements exists in Berlioz Form:

  • AbstractType: it's a form control
  • Group: represents an object in OOP
  • Collection: represents a collection of AbstractType or Group

Usage

Form creation

Constructor of Form object accept 3 parameters:

  • Name of form
  • Mapped object
  • Array of options

Example:

$form = new Form('my_form', null, ['method' => 'post']);

Declare form control

add method accept 3 parameters:

  • Name of control (must be the same that the mapped element)
  • Type (class name or object)
  • Array of options

Options are different between controls.

Example:

$form->add('my_control', Text::class, ['label' => 'My control']);

Handle

Berlioz Form implements PSR-7 (HTTP message interfaces). You must give the server request to the handle method.

$form = new Form('my_form', null, ['method' => 'post']);
// ...

$form->handle($request);

if ($form->isSubmitted() && $form->isValid()) {
    // ...
}

Group

Example for an postal address:

$form->add('address',
           (new Group)
               ->add('address', Text::class, ['label' => 'Address'])
               ->add('address_next', Text::class, ['label' => 'Address (next)', 'required' => false])
               ->add('postal_code', Text::class, ['label' => 'Postal code'])
               ->add('city', Text::class, ['label' => 'City']));

Collection

Example for a list of addresses:

$form->add('addresses', (new Collection(['prototype' =>
                                             (new Group(['type' => 'address']))
                                                 ->add('address', Text::class, ['label' => 'Address'])
                                                 ->add('address_next',
                                                       Text::class,
                                                       ['label'    => 'Address (next)',
                                                        'required' => false])
                                                 ->add('postal_code', Text::class, ['label' => 'Postal code'])
                                                 ->add('city', Text::class, ['label' => 'City']),
                                         'data_type' => '\ArrayObject'])))