A PHP random field name generator to manage forms

v1.0.0 2018-11-29 14:27 UTC


A PHP random field name generator to manage forms.

  • Needs PHP 7.1+
  • Supports array fields: field_name[]
  • Automatically gets the uploads from $_FILES


Using Composer:

composer require aymdev/form-builder

Generate form fields

Register the fields of your form:

use Phunder\FormBuilder\FormBuilder;

$form = new FormBuilder('form_name');

// Set fields

Then output their key in the form:

<form action="" method="post">
    <label for="<?= $form->getFieldKey('username'); ?>">Username</label>
    <input type="text" name="<?= $form->getFieldKey('username'); ?>">

    <label for="<?= $form->getFieldKey('password'); ?>">Password</label>
    <input type="password" name="<?= $form->getFieldKey('password'); ?>">

    <input type="submit" name="<?= $form->getFieldKey('login'); ?>" value="Login">

Or you can export all the keys:

$keys = $form->exportKeys();

echo $keys['username']; // outputs the generated field key

Fetch the form data

First, let's check if your form has been submitted:

use Phunder\FormBuilder\FormFetcher;

$form_data = new FormFetcher('form_name', 'post');

// Check if submit input has been found
if ($form_data->isFieldSet('login')) {
    # ...

Then fetch your fields separately or all at once:

$username = $form_data->getField('username');
$password = $form_data->getField('password');

$fields = $form_data->export();

FormBuilder uses the session to save the keys, clear it to remove the form data:


Uploading a file ?

Just fetch it as any other field:

$file = $form_data->getField('upload');