dennis84/formz

A simple form libary for PHP, inspired by the form component from the PlayFramework

dev-master 2014-03-12 17:36 UTC

This package is not auto-updated.

Last update: 2024-05-11 12:49:38 UTC


README

A simple form libary for PHP, inspired by the form component from the PlayFramework.

Build Status

Example

This is a quick example to give you a taste of what formz does.

<?php

$builder = new \Formz\Builder();

$form = $builder->form([
    // Creates a text field that must not be blank.
    $builder->field('name')->nonEmptyText(),
    // Creates a numeric field.
    $builder->field('age')->integer(),
], function ($name, $age) {
    // This is the apply function. Use this function to convert the submitted
    // data to your domain objects.
    return new User($name, $age);
}, function (User $user) {
    // This is the unapply function. Use it to convert your domain models into
    // an associative array. This function is only needed if you want to fill a 
    // form with existing values.
    return [
        'name' => $user->getName(),
        'age' => $user->getAge(),
    ];
});

// Binds the $_POST data to the form.
$form->bind($_POST);

if ($form->isValid()) {
    // Get the applied data. In this example, this is the "User" object.
    $user = $form->getData();
}

More examples

Formz has a pretty comprehensive test coverage that demonstrates the whole bunch of functionality.