palmtree/form

Form builder with Bootstrap v5/v4 classes, validation, Recaptcha support, AJAX submissions and more

v5.0.0 2024-05-02 14:16 UTC

README

License Build Packagist Version

PHP form builder with Bootstrap v5 and v4 classes, validation, data binding, Google Recaptcha support and other goodies

Requirements

  • PHP >= 8.1
  • jQuery (If you want to use Recaptcha and/or AJAX submissions)

Installation

Use composer to add the package to your dependencies:

composer require palmtree/form

Usage Example

Build

use Palmtree\Form\FormBuilder;
use Palmtree\Form\Captcha\GoogleRecaptcha;

$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>'),
    ]);

$builder->add('send_message', 'submit');

$form = $builder->getForm();
// Set $form to some variable accessible in a view

Render

<script src="/path/to/palmtree-form.pkgd.min.js"></script> <!-- Optional -->
<div class="container">
    <?= $form->render(); ?>
</div>

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