2017 © Pedro Peláez
 

library form-manager

PHP-HTML form manager

image

form-manager/form-manager

PHP-HTML form manager

  • Wednesday, September 27, 2017
  • by oscarotero
  • Repository
  • 13 Watchers
  • 76 Stars
  • 9,561 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 29 Forks
  • 1 Open issues
  • 62 Versions
  • 2 % Grown

The README.md

Form Manager

Latest Version on Packagist ![Software License][ico-license] Testing ![Total Downloads][ico-downloads], (*1)

Note: this is the documentation of FormManager 7.x

For v6.x version Click here, (*2)

Installation:

This package requires PHP>=7.2 and is available on Packagist:, (*3)

Supports symfony/validator v5, v6 and v7., (*4)

composer require form-manager/form-manager

Create a field

FormManager is namespaced, but you only need to import a single class into your context:, (*5)

use FormManager\Factory as F;

Use the imported factory to create all form elements:, (*6)

// Create an input type="text" element
$name = F::text();

// Create the input with a label
$name = F::text('Please, introduce your name');

// Or with extra attributes
$name = F::text('Please, introduce your name', ['class' => 'name-field']);

// Add or remove attributes
$name->setAttribute('title', 'This is the name input');
$name->removeAttribute('class');
$name->setAttributes([
    'required',
    'readonly',
    'tabindex' => 2,
    'maxlength' => 50
]);

// Set the value
$name->setValue('MyName');

// Use magic properties to get/set/remove attributes
$name->class = 'name-field';
$name->required = false;
unset($name->readonly);

List of all available inputs:

All HTML5 field types are supported:, (*7)

  • F::checkbox($label, $attributes)
  • F::color($label, $attributes)
  • F::date($label, $attributes)
  • F::datetimeLocal($label, $attributes)
  • F::email($label, $attributes)
  • F::file($label, $attributes)
  • F::hidden($value, $attributes)
  • F::month($label, $attributes)
  • F::number($label, $attributes)
  • F::password($label, $attributes)
  • F::radio($label, $attributes)
  • F::range($label, $attributes)
  • F::search($label, $attributes)
  • F::select($label, $options, $attributes)
  • F::submit($label, $attributes)
  • F::tel($label, $attributes)
  • F::text($label, $attributes)
  • F::textarea($label, $attributes)
  • F::time($label, $attributes)
  • F::url($label, $attributes)
  • F::week($label, $attributes)

Note that all inputs accepts the same arguments except hidden and select., (*8)

Validation

This library uses internally symfony/validation to perform basic html5 validations and error reporting. HTML5 validation attributes like required, maxlength, minlength, pattern, etc are supported, in addition to intrinsic validations assigned to each input like email, url, date, etc., (*9)

// Set global default error messages
F::setErrorMessages([
    'required' => 'The field is required'
    'maxlength' => 'The field is too long, it must have {{ limit }} characters or less',
]);

$email = F::email();

// Set per-fied error messages
$email->setErrorMessages([
    'email' => 'The email is not valid',
    'required' => 'The email is required',
    'maxlength' => 'The email is too long, it must have {{ limit }} characters or less',
]);

$email->setValue('invalid-email');

// Validate the value
if ($email->isValid()) {
    return true;
}

// Get the errors
$error = $email->getError();

// Print the first error message
echo $error;

// Iterate through all error messages
foreach ($error as $err) {
    echo $err->getMessage();
}

// And add more symfony constraints
$ip = F::text();
$ip->addConstraint(new Constraints\Ip());

See all supported constraints by symfony/validation., (*10)

Translations

This package allows you to set your custom Validation instance with Factory::setValidator()., (*11)

This allows you to use symfony/translations in order to have translations in place., (*12)

composer require symfony/translations
$validator = Validation::createValidatorBuilder()
    ->setTranslator($translator)
    ->setTranslationDomain('validators')
    ->getValidator();

// Set validator
F::setValidator($validator);

See examples/translations.php to see the full example., (*13)

Render html

$name = F::text('What is your name?', ['name' => 'name']);

echo $name;

```html , (*14)


Set a custom template using `{{ label }}` and `{{ input }}` placeholders: ```php $name->setTemplate('{{ label }} <div class="input-content">{{ input }}</div>'); echo $name;

```html , (*15)


If you want to wrap the previous template in a custom html, use the `{{ template }}` placeholder: ```php $name->setTemplate('<fieldset>{{ template }}</fieldset>'); echo $name;

```html , (*16)


## Grouping fields Group the fields to follow a specific data structure: ### Group Groups allow to place a set of inputs under an specific name: ```php $group = F::group([ 'name' => F::text('Username'), 'email' => F::email('Email'), 'password' => F::password('Password'), ]); $group->setValue([ 'name' => 'oscar', 'email' => 'oom@oscarotero.com', 'password' => 'supersecret', ]);

Radio group

Special case for radios where all inputs share the same name with different values:, (*17)

$radios = F::radioGroup([
    'red' => 'Red',
    'blue' => 'Blue',
    'green' => 'Green',
]);

$radios->setValue('blue');

If you need the radio group to be required, you should add the required attribute to at least one radio button:, (*18)

$radios = F::radioGroup([
    'red' => F::radio('Red', ['required' => true]),
    'blue' => 'Blue',
    'green' => 'Green',
]);

Submit group

Special case to group several submit buttons under the same name but different values:, (*19)

$buttons = F::submitGroup([
    'save' => 'Save the row',
    'duplicate' => 'Save as new row',
]);

$buttons->setName('action');

Group collection

Is a collection of values using the same group:, (*20)

$groupCollection = F::groupCollection(
    f::group([
        'name' => F::text('Name'),
        'genre' => F::radioGroup([
            'm' => 'Male',
            'f' => 'Female',
            'o' => 'Other',
        ]),
    ])
]);

$groupCollection->setValue([
    [
        'name' => 'Oscar',
        'genre' => 'm'
    ],[
        'name' => 'Laura',
        'genre' => 'f'
    ],
])

Multiple group collection

Is a collection of values using various groups, using the field type to identify which group is used by each row:, (*21)

$multipleGroupCollection = F::multipleGroupCollection(
    'text' => f::group([
        'type' => F::hidden(),
        'title' => F::text('Title'),
        'text' => F::textarea('Body'),
    ]),
    'image' => f::group([
        'type' => F::hidden(),
        'file' => F::file('Image file'),
        'alt' => F::text('Alt text'),
        'text' => F::textarea('Caption'),
    ]),
    'link' => f::group([
        'type' => F::hidden(),
        'text' => F::text('Link text'),
        'href' => F::url('Url'),
        'target' => F::select([
            '_blank' => 'New window',
            '_self' => 'The same window',
        ]),
    ]),
]);

$multipleGroupCollection->setValue([
    [
        'type' => 'text',
        'title' => 'Welcome to my page',
        'text' => 'I hope you like it',
    ],[
        'type' => 'image',
        'file' => 'avatar.jpg',
        'alt' => 'Image of mine',
        'text' => 'This is my photo',
    ],[
        'type' => 'link',
        'text' => 'Go to my webpage',
        'href' => 'https://oscarotero.com',
        'target' => '_self',
    ],
]);

Datalist

Datalists are also allowed, just use the createDatalist() method:, (*22)

$input = F::search();

$datalist = $input->createDatalist([
    'female' => 'Female',
    'male' => 'Male'
]);

echo $input;
echo $datalist;

Forms

We need a form to put all this things together., (*23)

$loginForm = F::form([
    'username' => F::text('User name'),
    'password' => F::password('Password'),
    '' => F::submit('Login'),
]);

$loginForm->setAttributes([
    'action' => 'login.php',
    'method' => 'post',
]);

// mLoad data from globals $_GET, $_POST, $_FILES
$loginForm->loadFromGlobals();

// Load data passing the arrays
$loginForm->loadFromArrays($_GET, $_POST, $_FILES);

// Or load from PSR-7 server request
$loginForm->loadFromServerRequest($serverRequest);

// Get loaded data
$data = $loginForm->getValue();

// Print the form
echo $loginForm;

// Access to specific inputs:
echo $loginForm->getOpeningTag();
echo '

Login:

'; echo $loginForm['username']; echo '
'; echo $loginForm['password']; echo '
'; echo $loginForm['']; echo $loginForm->getClosingTag(); echo $loginForm->getOpeningTag(); echo '

Login:

'; //nIterate through all inputs foreach ($loginForm as $input) { echo "
{$input}
"; } echo $loginForm->getClosingTag();

Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details., (*24)

The MIT License (MIT). Please see LICENSE for more information., (*25)

The Versions

27/09 2017

dev-master

9999999-dev https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

27/09 2017

v5.1.3

5.1.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

11/05 2017

v5.1.2

5.1.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

27/09 2016

v5.1.1

5.1.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

12/09 2016

v5.1.0

5.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

28/06 2016

v5.0.4

5.0.4.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

18/05 2016

v5.0.3

5.0.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

11/05 2016

v5.0.2

5.0.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

04/05 2016

v5.0.1

5.0.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

04/05 2016

v5.0

5.0.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

29/04 2016

4.x-dev

4.9999999.9999999.9999999-dev https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

14/04 2016

v4.7.3

4.7.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

06/10 2015

v4.7.2

4.7.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

06/10 2015

v4.7.1

4.7.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

12/08 2015

4.7.0

4.7.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

12/07 2015

v4.6.2

4.6.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

12/07 2015

v4.6.1

4.6.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

08/07 2015

v4.6.0

4.6.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

16/06 2015

v4.5.0

4.5.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 form validator html data

30/05 2015

v4.4.0

4.4.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

23/05 2015

v4.3.9

4.3.9.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

19/05 2015

v4.3.8

4.3.8.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

13/05 2015

v4.3.7

4.3.7.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

08/05 2015

v4.3.6

4.3.6.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

30/04 2015

v4.3.5

4.3.5.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

18/04 2015

v4.3.4

4.3.4.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

16/04 2015

v4.3.3

4.3.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

12/04 2015

v4.3.2

4.3.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

25/03 2015

v4.3.1

4.3.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

13/03 2015

v4.3

4.3.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

09/03 2015

v4.2

4.2.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

05/03 2015

v4.1

4.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

26/02 2015

v4.0.1

4.0.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

26/02 2015

v4.0

4.0.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

form validator html data

08/01 2015

v3.10.1

3.10.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

08/01 2015

v3.10

3.10.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

05/11 2014

v3.9.4

3.9.4.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

16/10 2014

v3.9.3

3.9.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

08/10 2014

v3.9.2

3.9.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

06/10 2014

v3.9.1

3.9.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

20/09 2014

v3.9.0

3.9.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

17/09 2014

v3.8.3

3.8.3.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

15/09 2014

v3.8.2

3.8.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

14/09 2014

v3.8.1

3.8.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

14/09 2014

v3.8.0

3.8.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

30/07 2014

v3.7.1

3.7.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

29/07 2014

v3.7.0

3.7.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

22/07 2014

v3.6.2

3.6.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

21/07 2014

v3.6.1

3.6.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

06/02 2014

v3.6.0

3.6.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

02/02 2014

v3.5.2

3.5.2.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

31/01 2014

v3.5.1

3.5.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

02/01 2014

v3.5.0

3.5.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

09/12 2013

v3.4.1

3.4.1.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

09/12 2013

v3.4.0

3.4.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

22/11 2013

v3.1

3.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

18/11 2013

v3.0

3.0.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

14/11 2013

v2.1

2.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

12/11 2013

v2.0

2.0.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

17/10 2013

v1.1

1.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

09/10 2013

v1.0

1.0.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data

09/07 2013

v0.1

0.1.0.0 https://github.com/oscarotero/form-manager

PHP-HTML form manager

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.3.0

 

form validator html data