chtombleson/formster

Form generation and validation library

0.1 2016-03-20 22:22 UTC

This package is not auto-updated.

Last update: 2024-05-03 16:55:35 UTC


README

A PHP Form builder and validation library.

Installation

composer require chtombleson/formster

Usage

Here is a simple example of how to use the class. The handle request function will take any PSR-7 compliant request object.

<?php
use Formster\Form;

$form = new Form();

$form->addField([
    'name' => 'username',
    'type' => 'text',
    'validate' => ['not_empty'],
])
->addField([
    'name' => 'password',
    'type' => 'password',
    'validate' => ['not_empty'],
])
->addField([
    'name' => 'login',
    'type' => 'submit',
    'value' => 'Log In',
]);

if (strtolower($request->getMethod()) == 'post') {
    $form->handleRequest($request);

    if (!$form->isValid()) {
        var_dump($form->getErrors());
    } else {
        var_dump($form->getData());
    }
} else {
    echo $form->renderForm();
}