2017 © Pedro Peláez
 

library formulaic

Monolyth unframework object oriented form tools

image

monolyth/formulaic

Monolyth unframework object oriented form tools

  • Sunday, April 22, 2018
  • by monomelodies
  • Repository
  • 1 Watchers
  • 0 Stars
  • 419 Installations
  • PHP
  • 1 Dependents
  • 2 Suggesters
  • 0 Forks
  • 0 Open issues
  • 81 Versions
  • 6 % Grown

The README.md

Formulaic

Object-oriented form utilities for PHP8.1+, (*1)

HTML forms suck. Well, no, they're superduper handy, but writing them and validating them server-side can be a pain. Formulaic offers a set of utilities to ease that pain., (*2)

Basic usage

Define a form with some fields and other requirements:, (*3)

isRequired();
        $this[] = new Submit('Go!', 'submit');
    }
}
```

In your template, either use the API to manually tweak your output, or simply
`__toString` the form to use the defaults:

```php



=$form[0]?> =$form['q']?>

To validate your form:, (*4)

valid()) {
    // ...Perform the search...
}
```

To get a list of errors:

```php
errors()) {
    // ...Do error handling, or give feedback...
}
```

Forms can contain fieldsets:

```php

    =$form['Global search']?>
    <?=$form['Search by ID']?>
    <?=$form['submit']?>
</form>

Custom elements in forms

Simply add strings to the form; they will be outputted verbatim:, (*5)

<?php

// ...
class Form extends Get
{
    public function __construct()
    {
        $this[] = new Radio('foo');
        $this[] = '

custom HTML element!

'; } }

Under the hood

As you will have guessed, the Post and Get forms look at posted and get data respectively. This means any matching data in the superglobal (which, for Post, includes $_FILES) is automagically set on the form. For elements in groups (excluding fieldsets), Formulaic assumes they will be in a sub-array:, (*6)

<?php

use Monolyth\Formulaic\{ Get, Element\Group, Text };

class Form extends Get
{
    public function __construct()
    {
        $this[] = new Group('foo', function ($group) {
            $group[] = new Text('bar');
        });
    }
}

This will match $_GET['foo']['bar'] for a value., (*7)

For checkbox groups (a set of related checkboxes, e.g. for settings), the values are presumed to be in their own array. E.g. with a checkbox group named 'foo' the values will be passed as $_POST['foo'] = [1, 2, 3]., (*8)

Adding tests

Form elements can contain tests, which the vaild() and error() methods use to produce output. A number of tests (like isRequired()) are pre-supplied, but you can easily add your own via the addTest method on elements:, (*9)

<?php

$input = new Text('foo');
$input->addTest(fn ($value) => $value == 'bar');

The above test will fail unless the user enters "bar" into the text input., (*10)

Binding models

Where Formulaic also really shines is in propagating the form data to your models. All the boilerplate code containing numerous isset calls? Gone!, (*11)

Your model is an object. Literally any object. What you want is for any property's previously filled value to be automatically set on your form, and for any value entered by the user to be updated on the object (which you can then persist to a database or whatever, that's up to you). Guess what? It's easy!, (*12)

<?php

class MyFrom extends Post
{
    //... define the form
    public function __construct()
    {
        $this[] = new Text('foo');
    }
}

$model = new stdClass;
$model->foo = 'bar';
$form = new MyForm;
$form->bind($model);

In the above example, the form in question - when __toStringed - will have a default value of "bar" for the foo input. If $_POST['foo'] happens to contain "buzz", it will instead contain that. Even better, after the call to bind it will also be so that $model->foo === 'buzz' equals true. Awesome! That's a gazillion lines of code you no longer have to think about!, (*13)

Binding can be done on any level, just remember that it needs to be on an object and that its (sub)properties must match the element's names., (*14)

You'll notice that this ties the model structure to the form buildup; however, that doesn't matter. The form elements are displayed "as is", it's just their names that need to match the model., (*15)

Transforming data

In the real world, model objects are often a lot more complicated than HTML forms, which basically deal with strings. Enter transformers: Formulaic's way of converting data to and from your models., (*16)

All elements support the withTransformer method, which basically accepts a callable. The idea here is that the callable's argument is type hinted (so as to determine which transformer to use) and it returns a suitable value based on that type. An acceptable transformer for a certain situation might be:, (*17)

<?php

class MyModel
{
    public Foo $foo;
}

class MyForm extends Post
{
    public function __construct()
    {
        $this[] = (new Text('foo'))
            ->withTransformer(fn(string $value) => new Foo($value));
    }
}

$model = new MyModel;
$form = new MyForm;
$form->bind($model);
echo get_class($model->foo); // Foo

You can define multiple transformers in one go with the withTransformers method (note the plural). Each argument is a callable., (*18)

Typically, you'll need two transformers: one from the model to the form, and one from the form back to the model. In some cases, the input may vary depending on the complexity of your project; define as many transformers as you need., (*19)

The input type hint may be a union in which case the transformer is valid for multiple types. Intersection type hints are not supported as they wouldn't really make sense in a transformation context., (*20)

GET forms gotcha!

In PHP, it is not possible to distinguish a "regular" page load from one triggered by a submitted GET form (unlike POST requests). To determine whether or not the user supplied values, Formulaic simply checks $_GET for truthiness. Note that this may or may not be sufficient for your needs; theoretically, an entirely empty form could be submitted which should still trigger this, or the URL may already contain (unrelated) GET-parameters., (*21)

In these corner cases, extend the Get form and implement your own wasSubmitted method doing an alternative check, e.g. for a hidden form field or a named submit button., (*22)

For post forms, the user is assumed to have supplied values whenever $_SERVER['REQUEST_METHOD'] == 'POST'. Again, situations may arise where this check is not good enough for you - extend and override, again., (*23)

Other methods than GET or POST

In casu, PUT or DELETE may crop up. These are not supported out of the box, since Formulaic is an HTML form library, and HTML only supports GET and POST. However, it is not unthinkable you would like to use validation and binding logic in controllers handling AJAX calls for instance. In that case, feel free to extend your own form class., (*24)

Any extension will want to implement the getSource method, e.g. for PUT you will manually parse file_get_contents('php://input'). Since we can't know what that contains (likely JSON, but assumption is the mother of all fuckup...) you'll need to write your own implementation suiting your needs., (*25)

Similarly, DELETE can contain GET data in the URL. We're not really sure when this would be necessary, but there you have it - a custom Delete form would likely extend GET., (*26)

The Versions

22/04 2018

1.9.1

1.9.1.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/03 2018

dev-master

9999999-dev

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/03 2018

1.9.0

1.9.0.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.2

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

08/02 2018

1.8.4

1.8.4.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/02 2018

1.8.3

1.8.3.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/02 2018

1.8.2

1.8.2.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/02 2018

1.8.1

1.8.1.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/02 2018

1.8.0

1.8.0.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

01/02 2018

1.7.12

1.7.12.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

30/01 2018

1.7.11

1.7.11.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

27/01 2018

1.7.10

1.7.10.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

27/01 2018

1.7.9

1.7.9.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

23/01 2018

1.7.8

1.7.8.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

23/01 2018

1.7.7

1.7.7.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

23/01 2018

1.7.6

1.7.6.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

22/01 2018

1.7.5

1.7.5.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

22/01 2018

1.7.4

1.7.4.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

21/01 2018

1.7.3

1.7.3.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

17/01 2018

1.7.2

1.7.2.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/01 2018

1.7.1

1.7.1.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/01 2018

1.7.0

1.7.0.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

08/01 2018

1.6.20

1.6.20.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/01 2018

1.6.19

1.6.19.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/01 2018

1.6.18

1.6.18.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

22/12 2017

1.6.17

1.6.17.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

21/12 2017

1.6.16

1.6.16.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

06/06 2017

1.6.15

1.6.15.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

05/06 2017

1.6.14

1.6.14.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

05/06 2017

1.6.13

1.6.13.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

02/06 2017

1.6.12

1.6.12.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

28/04 2017

1.6.11

1.6.11.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

08/02 2017

dev-develop

dev-develop

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

08/02 2017

1.6.10

1.6.10.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

30/11 2016

1.6.9

1.6.9.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/11 2016

1.6.8

1.6.8.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/11 2016

1.6.7

1.6.7.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

09/11 2016

1.6.6

1.6.6.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

06/11 2016

1.6.5

1.6.5.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

05/11 2016

1.6.4

1.6.4.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

25/09 2016

1.6.3

1.6.3.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

17/07 2016

1.6.2

1.6.2.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

17/07 2016

1.6.1

1.6.1.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

16/07 2016

1.6.0

1.6.0.0

Monolyth unframework object oriented form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo monolyth

10/06 2016

1.5.11

1.5.11.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

10/06 2016

1.5.10

1.5.10.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

04/06 2016

1.5.9

1.5.9.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

30/05 2016

1.5.8

1.5.8.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

25/05 2016

1.5.7

1.5.7.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

20/03 2016

1.5.6

1.5.6.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

06/03 2016

1.5.5

1.5.5.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

23/02 2016

1.5.4

1.5.4.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

08/02 2016

1.5.3

1.5.3.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

03/01 2016

1.5.2

1.5.2.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

30/12 2015

1.5.1

1.5.1.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

16/10 2015

1.5.0

1.5.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

26/09 2015

1.4.3

1.4.3.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

06/09 2015

1.4.2

1.4.2.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

27/08 2015

1.4.1

1.4.1.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

27/08 2015

1.4.0

1.4.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

08/08 2015

1.3.0

1.3.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

08/08 2015

1.2.3

1.2.3.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

07/08 2015

1.2.2

1.2.2.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

25/07 2015

1.2.1

1.2.1.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

25/07 2015

1.2.0

1.2.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

23/07 2015

1.1.0

1.1.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

21/07 2015

1.0.2

1.0.2.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/06 2015

1.0.1

1.0.1.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php form validation html5 oo

04/06 2015

1.0.0

1.0.0.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

30/03 2015

0.0.13

0.0.13.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.12

0.0.12.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.11

0.0.11.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.10

0.0.10.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.9

0.0.9.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.8

0.0.8.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

22/01 2015

0.0.7

0.0.7.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

21/01 2015

0.0.6

0.0.6.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

21/01 2015

0.0.5

0.0.5.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

21/01 2015

0.0.4

0.0.4.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php form validation html5 oo

20/01 2015

0.0.3

0.0.3.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

20/01 2015

0.0.2

0.0.2.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

20/01 2015

0.0.1

0.0.1.0

Object oriented PHP5 form tools

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst