2017 © Pedro Peláez
 

library entityform

image

asgard/entityform

  • Monday, February 12, 2018
  • by leyou
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

EntityForm

Build Status, (*1)

Entityform help you generate forms from entities. It creates the form fields corresponding to all the entity properties. Entityform is a sub-class of Form., (*2)

, (*3)

Installation

If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries., (*4)

composer require asgard/entityform 0.*

, (*5)

Usage in the Asgard Framework

The advantage of using the form service is that it will provides the Form with all the necessary dependencies:, (*6)

$request = \Asgard\Http\Request::CreateFromGlobals();
$entity  = new Article;
$container->make('entityForm', [
    $entity, #required
    [ #optional
        'action'  => 'form.php',
        'enctype' => 'multipart/form-data',
        'attrs'   => [
            'class' => 'formClass'
        ]
    ],
    $request, #optional, Asgard can provide the form with the current request
]);

The container is often accessible as a method parameter or through a ContainerAware object. You can also use the singleton but it is not recommended., (*7)

, (*8)

Usage outside the Asgard Framework

Here you will have to provide the dependencies yourself (see the next section):, (*9)

$entityFieldsSolver = new \Asgard\Entityform\EntityFieldsSolver;
$request            = \Asgard\Http\Request::CreateFromGlobals();
$entity             = new Article;
$form = new \Asgard\Entityform\Entityform(
    $entity, #required
    [ #optional
        'action'  => 'form.php',
        'enctype' => 'multipart/form-data',
        'attrs'   => [
            'class' => 'formClass'
        ]
    ],
    $request, #optional, if not request is provided the form will automatically use \Asgard\Http\Request::createFromGlobals()
    $entityFieldsSolver #optional, Asgard can automatically retrieve an instance of EntityFieldsSolver
);

, (*10)

Add entity relations

If the entity form was created from an entity having a "comments" relation, you embed it in the form with:, (*11)

$form->addRelation('comments');

This will add a field for selecting comments related to the entity. Works for all kinds of relations, "one" and "many"., (*12)

, (*13)

Save the entity

To save the entity, simple do:, (*14)

$form->save();

If there is a validation error, it will throw the exception \Asgard\Form\FormException. Refer to the Form documentation to know how to handle exceptions and errors., (*15)

, (*16)

Get the entity

$form->getEntity();

, (*17)

EntityFieldSolver

In order to tell the entityform how to create fields from entity properties, you can use the Asgard\Entityform\EntityFieldsSolver class. By default it already handles Text, text, Double, Integer, Email, Boolean, Date, Datetime, File entity properties (all in Asgard\Entity\Property\). If the EntityFieldsSolver does not know what type of field to create for a specific property, it will return a \Asgard\Form\Field\TextField field by default., (*18)

To extend the entityFieldsSolver add a callback which will return a form field object:, (*19)

$cb = function(\Asgard\Entity\Property $property) {
    if(get_class($property) == 'Asgard\Entity\Property\DateProperty')
        return new MyOwnDateField;
};
$fieldsSolver->add($cb);

For entity properties with multiple values, use:, (*20)

$cb = function(\Asgard\Entity\Property $property) {
    if(get_class($property) == 'Asgard\Entity\Property\DateProperty')
        return new \Asgard\Form\DynamicGroup;
};
$fieldsSolver->addMany($cb);

If the callback returns null it will be ignored., (*21)

You can also nest other solvers:, (*22)

$anotherFieldsSolver->addSolver($fieldsSolver);

If $anotherFieldsSolver cannot solve the field, it will ask to the nested solvers., (*23)

To solve a field from an entity property:, (*24)

$form->solve($Definition->getProperty('title'));

To get the EntityFieldSolver from a form:, (*25)

$fieldsSolver = $form->getEntityFieldsSolver();

To set the EntityFieldsSolver for a form:, (*26)

$form->addEntityFieldsSolver($cb);

Contributing

Please submit all issues and pull requests to the asgardphp/asgard repository., (*27)

License

The Asgard framework is open-sourced software licensed under the MIT license, (*28)

The Versions

12/02 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michel Hognerud

13/05 2016

v0.3.1

0.3.1.0

  Sources   Download

MIT

The Requires

 

by Michel Hognerud

12/05 2016

v0.3.0

0.3.0.0

  Sources   Download

MIT

The Requires

 

by Michel Hognerud

13/06 2015

v0.2.0

0.2.0.0

  Sources   Download

MIT

The Requires

 

by Michel Hognerud

09/09 2014

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

 

by Michel Hognerud