palmtree / form
Form builder with Bootstrap v5/v4 classes, validation, Recaptcha support, AJAX submissions and more
Installs: 1 852
Dependents: 1
Suggesters: 0
Security: 0
Stars: 35
Watchers: 4
Forks: 8
Open Issues: 0
Requires
- php: >=8.1
- palmtree/argparser: ^2.1
- palmtree/html: ^4.0.2
- palmtree/nameconverter: ^2.0
Requires (Dev)
- palmtree/php-cs-fixer-config: ^2.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.2
Suggests
- ext-curl: For Google Recaptcha support
- ext-fileinfo: For mime type upload validation
- ext-json: For Google Recaptcha support
This package is auto-updated.
Last update: 2024-11-02 10:13:53 UTC
README
PHP form builder with Bootstrap classes, validation, data binding, Google Recaptcha and hCaptcha support, plus other goodies.
Requirements
- PHP >= 8.1
Installation
Use composer to add the package to your dependencies:
composer require palmtree/form
Optional: Install the NPM package for AJAX form submission, CAPTCHA support and form collections:
npm install @palmtree/form
Usage Example
Build
use Palmtree\Form\FormBuilder; use Palmtree\Form\Captcha\GoogleRecaptcha; use Palmtree\Form\Captcha\HCaptcha; $builder = (new FormBuilder('my_form')) ->add('name', 'text', ['error_message' => 'Please enter your name']) ->add('email_address', 'email', [ 'help' => 'We will never share your email with anyone', ]) ->add('message', 'textarea', [ 'required' => false, 'label' => 'Enter your message', ]) ->add('recaptcha', 'captcha', [ 'captcha' => new GoogleRecaptcha('<site_key>', '<secret>'), //'captcha' => new HCaptcha('<site_key>', '<secret>'), ]); $builder->add('send_message', 'submit'); $form = $builder->getForm(); // Set $form to some variable accessible in a view
Render
<div class="container"> <?= $form->render(); ?> </div> <!-- Optional JS for AJAX submissions, CAPTCHA support and form collections --> <script src="https://unpkg.com/@palmtree/form@6.1.0/dist/palmtree-form.pkgd.min.js"></script>
Process
$form->handleRequest(); if ($form->isSubmitted() && $form->isValid()) { // Send an email/save to database etc $name = $form->get('name')->getData(); }
See the examples directory for examples using AJAX, file uploads, collections and more.
Documentation
View the documentation for more advanced usage.
Examples
The simplest way to run the examples is run the serve.sh script. This script starts a small PHP Docker container and serves the examples using PHP's built-in web server.
./examples/serve.sh
License
Released under the MIT license