library rka-slim-zendform
Slim Framework ZF2 form service provider
akrabat/rka-slim-zendform
Slim Framework ZF2 form service provider
- Tuesday, May 3, 2016
- by akrabat
- Repository
- 2 Watchers
- 1 Stars
- 25 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 2 Forks
- 0 Open issues
- 2 Versions
- 4 % Grown
This service provider integrates Zend\Form into a Slim 3 application., (*1)
Usage
composer require slim/twig-view
composer require akrabat/rka-slim-zendform
- Register Twig-View as per the README
-
Register the FormProvider
in index.php:, (*2)
$app->getContainer()->register(new RKA\Form\FormProvider);
-
Create a form:, (*3)
<?php
namespace RKA;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
class ExampleForm extends Form implements InputFilterProviderInterface
{
public function init()
{
$this->add([
'name' => 'email',
'options' => [
'label' => 'Email address',
],
'attributes' => [
'id' => 'email',
'class' => 'form-control',
'required' => 'required',
],
]);
$this->add([
'name' => 'submit',
'type' => 'button',
'options' => [
'label' => 'Go!',
],
'attributes' => [
'class' => 'btn btn-default',
],
]);
}
public function getInputFilterSpecification()
{
return [
'email' => [
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags'],
],
'validators' => [
['name' => 'EmailAddress'],
],
],
];
}
}
-
Example action:, (*4)
$app->map(['GET', 'POST'], '/', function ($request, $response) {
$sm = $this['serviceManager'];
$formElementManager = $sm->get('FormElementManager');
$form = $formElementManager->get("RKA\ExampleForm");
if ($request->isPost()) {
$data = $request->post();
$form->setData($data);
$isValid = $form->isValid();
if ($form->isValid()) {
echo "Success!";
exit;
}
}
$this['view']->render($response, 'home.twig', array(
'form' => $form
));
return $response;
});
dev-master
9999999-dev
http://akrabat.com
Slim Framework ZF2 form service provider
Sources
Download
BSD-3-Clause
The Requires
framework
form
zf2
slim
provider
zend-form