asgard / entityform
Requires
- php: >=5.5.9
- asgard/entity: ~0.3.0
- asgard/form: ~0.3.0
This package is not auto-updated.
Last update: 2024-12-21 08:14:46 UTC
README
#EntityForm
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.
- Installation
- Usage in the Asgard Framework
- Usage outside the Asgard Framework
- Add entity relations
- Save the entity
- Get the entity
- EntityFieldSolver
##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.
composer require asgard/entityform 0.*
##Usage in the Asgard Framework The advantage of using the form service is that it will provides the Form with all the necessary dependencies:
$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.
##Usage outside the Asgard Framework Here you will have to provide the dependencies yourself (see the next section):
$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
);
If the entity form was created from an entity having a "comments" relation, you embed it in the form with:
$form->addRelation('comments');
This will add a field for selecting comments related to the entity. Works for all kinds of relations, "one" and "many".
To save the entity, simple do:
$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.
$form->getEntity();
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.
To extend the entityFieldsSolver add a callback which will return a form field object:
$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:
$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.
You can also nest other solvers:
$anotherFieldsSolver->addSolver($fieldsSolver);
If $anotherFieldsSolver cannot solve the field, it will ask to the nested solvers.
To solve a field from an entity property:
$form->solve($Definition->getProperty('title'));
To get the EntityFieldSolver from a form:
$fieldsSolver = $form->getEntityFieldsSolver();
To set the EntityFieldsSolver for a form:
$form->addEntityFieldsSolver($cb);
###Contributing
Please submit all issues and pull requests to the asgardphp/asgard repository.
License
The Asgard framework is open-sourced software licensed under the MIT license