zenbox / form
Form HTML builder standalone component
1.0.1
2021-09-05 05:18 UTC
Requires
- php: ^7.4
- psr/http-message: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.6
- laminas/laminas-validator: ^2.14
- phpunit/phpunit: ^9.3
- symfony/validator: ^5.3
Suggests
- laminas/laminas-validator: Laminas validator component for form validation
- symfony/validator: Symfony validator component for form validation
This package is auto-updated.
Last update: 2024-12-05 12:21:14 UTC
README
Form HTML builder standalone component. Bootstrap 4 form renderer. Symfony or Laminas validation.
Installation
Using Composer:
composer require zenbox/form
Configuration
Use the static method for the rendering and validation configuration
// or Bootstrap4 and Symfony Validator Form::configuration(\ZenBox\Form\Renderer\Bootstrap4FormRenderer::class, \ZenBox\Form\Validator\SymfonyFormValidator::class); // or Bootstrap4 and Laminas Validator Form::configuration(\ZenBox\Form\Renderer\Bootstrap4FormRenderer::class, \ZenBox\Form\Validator\LaminasFormValidator::class);
Instantiation
<?php use ZenBox\Form\FormBuilder; // Create new form $form = FormBuilder::create() ->text('name', 'Name') ->text('email', 'Email') ->password('password', 'Password')->autocomplete('new-password') ->submit('Registration') ->build(); // Set Data $form->setData(['name' => 'User']); // Set Errors $form->setErrors(['password' => 'Password required']); // Render form echo $form; // Render element echo $form->getField('name');
Examples
PSR-7 Request Handler
<?php declare(strict_types=1); namespace App\RequestHandler; use Laminas\Diactoros\Response\HtmlResponse; use Laminas\Diactoros\Response\RedirectResponse; use Mezzio\Authentication\AuthenticationInterface; use Mezzio\Helper\UrlHelper; use Mezzio\Template\TemplateRendererInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotCompromisedPassword; use ZenBox\Form\FormBuilder; final class RegistrationHandler implements RequestHandlerInterface { private TemplateRendererInterface $templateRenderer; private FormBuilder $formBuilder; private RegistrationUserCommandHandler $registrationUserCommandHandler; private AuthenticationInterface $authentication; private UrlHelper $urlHelper; public function __construct( TemplateRendererInterface $templateRenderer, FormBuilder $formBuilder, RegistrationUserCommandHandler $registrationUserCommandHandler, AuthenticationInterface $authentication, UrlHelper $urlHelper ) { $this->templateRenderer = $templateRenderer; $this->formBuilder = $formBuilder; $this->registrationUserCommandHandler = $registrationUserCommandHandler; $this->authentication = $authentication; $this->urlHelper = $urlHelper; } public function handle(ServerRequestInterface $request): ResponseInterface { $form = $this->formBuilder->create() ->text('name', 'Name')->constrains(new NotBlank()) ->text('email', 'Email')->constrains(new NotBlank(), new Email()) ->password('password', 'Password')->autocomplete('new-password')->constrains(new NotCompromisedPassword()) ->submit('Registration') ->build(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Get form data and registration user $data = $form->getData(); // Registration user... return new RedirectResponse($this->urlHelper->generate('registration-success')); } return new HtmlResponse($this->templateRenderer->render('views::registration', [ 'form' => $form, ])); } }
Render Form
Twig Renderer
// entire form {{ form|raw }} // separate elements {{ form.field('email')|raw }}
PHP Renderer
<?php use ZenBox\Form\Form; /** @var $form Form */ // entire form echo $form; // separate elements echo $form->getField('email');