Search by

assegaiphp / forms

amasiye

Typed form decoding, binding, validation, and HTML rendering for modern PHP applications.

Package info

github.com/assegaiphp/forms

pkg:composer/assegaiphp/forms

Statistics

Installs: 1 249

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

0.11.0 2026-11-16 11:44 UTC

README

Assegai Logo

Latest release Tests PHP 8.4+ License Status active

Typed form decoding, binding, validation, and HTML rendering for modern PHP applications.

AssegaiPHP Forms

assegaiphp/forms manages HTML forms submitted through GET, POST, PUT, or PATCH requests. It provides typed form controls, submitted-data decoding and binding, server-side validation, and HTML rendering. The package integrates with AssegaiPHP but remains focused on forms rather than the framework runtime.

Requirements

  • PHP 8.4 or newer

Contribution workflow

For commit and pull request conventions in this repo, see:

Git hooks for this repository live in .githooks. Running composer install or composer update will automatically configure core.hooksPath for this clone so the committed pre-push checks are used. If you need to apply the hook configuration manually, run:

composer run hooks:install

Features

  • Form Creation: Easily create HTML forms programmatically using a simple and intuitive syntax.
  • Form Fields: Support for various types of form fields such as text fields, numeric fields, and more.
  • Data Binding: Automatically populate form fields with data from your models or arrays.
  • Validation: Define validation rules for form fields and perform server-side validation effortlessly.
  • Error Handling: Automatically retrieve validation errors for form fields.
  • Customization: Highly customizable rendering and extending capabilities.
  • Compatibility: Integrates with AssegaiPHP and can be used in other modern PHP applications.

Installation

You can install the AssegaiPHP Forms Library using Composer:

composer require 'assegaiphp/forms:~0.11.0'

Quick Start

  1. Create a Form:
    use Assegai\Forms\Form;
    use Assegai\Forms\Enumerations\HttpMethod;
    
    $form = new Form(
        method: HttpMethod::POST,
        selector: '#contact-form'
    );
  2. Add Form Fields:
    $form->set('name', '');
    $form->set('email', '');
    $form->set('message', '');
  3. Process Form Submission:
    if ($form->isSubmitted()) {
        // Validate the form
        $form->validate();
        if ($form->isValid()) {
            // Process the form data
            $data = $form->getData();
            // ...
        } else {
            $errors = $form->getErrors();
            // Handle validation errors
        }
    }
  4. Render the Form:
    echo $form->render();

Advanced Usage

Adding Validation Rules

use Assegai\Forms\Form;
use Assegai\Forms\Enumerations\HttpMethod;
use Assegai\Forms\FormControls\TextField;

$form = new Form(method: HttpMethod::POST, selector: '#user-form');

// Create a field with validation rules
$nameField = new TextField('name', '', ['required', 'min:3']);
$form->addField($nameField);

// Or add fields and set validation rules later
$form->set('email', '');
$emailField = $form->getField('email');
$emailField->addValidationRules('required', 'email');

Getting Form Data

// Get data as an associative array
$data = $form->getData();

// Get data as a stdClass object
$dataObject = $form->getData(asObject: true);

Working with Individual Fields

// Check if a field exists
if ($form->has('name')) {
    // Get a specific field value
    $name = $form->getFieldValue('name');

    // Get the field object
    $nameField = $form->getField('name');

    // Remove a field
    $form->removeField('name');
}

Form HTTP Methods

The form supports multiple HTTP methods:

use Assegai\Forms\Enumerations\HttpMethod;

// POST forms
$postForm = new Form(method: HttpMethod::POST, selector: '#form');

// GET forms
$getForm = new Form(method: HttpMethod::GET, selector: '#search');

// PUT/PATCH forms for updates
$updateForm = new Form(method: HttpMethod::PUT, selector: '#update-form');
$patchForm = new Form(method: HttpMethod::PATCH, selector: '#patch-form');

Form Selector

The selector parameter can be used to set the form's ID, CSS class, or action URL:

// Set the form ID
$form = new Form(selector: '#my-form');

// Set the form CSS classes
$form = new Form(selector: '.form-class1.form-class2');

// Set the form action URL
$form = new Form(selector: '/submit-form');

For framework integration guidance, see the AssegaiPHP guide.

Contributing

We welcome contributions from the community! If you'd like to contribute to the AssegaiPHP Forms Library, please follow our Contribution Guidelines.

License

The AssegaiPHP Forms Library is open-source software licensed under the MIT License.