2017 © Pedro Peláez
 

library perforated

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

image

burntcaramel/perforated

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  • Tuesday, May 5, 2015
  • by BurntCaramel
  • Repository
  • 2 Watchers
  • 3 Stars
  • 14 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Perforated – simple yet pretty powerful forms

Perforated forms are easy to create, read, and change later on. Define your form’s structure using a simple set up of key-based arrays, in PHP or JSON., (*1)

Edit easily.

The easy-to-understand syntax means you can quickly create a form in a few minutes. Use HTML5 entry types such as URL, email address, number, and checkbox. They are automatically validated on the server with no extra code., (*2)

Come back to it later and add, edit, remove. Adding new entries means just adding a few lines of simple code. The syntax is so simple you can even define your form in a JSON file, if you wish., (*3)

Style sensibly.

Perforated is designed to be smart. It groups related entries together in <fieldset> elements. Entries are automatically created with an associated <label>. This means it is very easy to style in CSS. Target specific form entries. Target all entries of a certain type., (*4)

Extend.

Perforated will automatically shows type-specific errors for incorrectly entered fields. Easily extend this validation. Add your own error messages., (*5)

Form processing and validation is completely separate from form display, so extend or replace how your form is validated or how it is displayed however you wish., (*6)

Automatically namespaced.

Submitted forms have their own namespace e.g. $_POST['formID'] This means it will not clash with other POST variables, so use fields like name without worry in WordPress: http://stackoverflow.com/questions/15810948/post-returns-empty-on-form-submit-in-wordpress, (*7)

Make sections dependant on whatever you wish.

Sections can be made to only show when a checkbox is on with one line. Just declare what you want, there's no extra JS., (*8)

External values can be used to automatically fill entries or for dependencies to turn sections on and off, just by using a simple callback. For example, only show and use a particular part of a form if the user is logged in – no complicated if-statements., (*9)

Example

define ('EXAMPLE_SUBMIT_VIDEO', 'submitVideo');
define ('BURNT_ENDASH', "\xE2\x80\x93");

// This can be written using PHP arrays, or loaded from a JSON file.
$formOptions = array(
    'baseID' => EXAMPLE_SUBMIT_VIDEO,
    // All the entries to be used in the form.
    'entries' => array(
        'videoLink' => array(
            'title' => 'Video link '.BURNT_ENDASH.' Vimeo / YouTube',
            'value' => '',
            'type' => 'url',
            'required' => true
        ),
        'story' => array(
            'title' => 'Video details & story', // Don't need to worry about escaping HTML first.
            'value' => '',
            'type' => 'text',
            'multipleLines' => true,
            'required' => true
        ),
        'name' => array(
            'title' => 'Your name',
            'value' => '',
            'type' => 'text',
            'required' => true
        ),
        'email' => array(
            'title' => 'Your email',
            'value' => '',
            'type' => 'email',
            'required' => true
        ),
        'helpedCreate' => array(
            'title' => 'Were you a part of this video?',
            'value' => false,
            'type' => 'checkbox',
            'titleWhenOff' => 'No',
            'titleWhenOn' => 'Yes'
        ),
        'role' => array(
            'title' => 'What was your role?',
            'value' => '',
            'type' => 'text',
            'required' => true
        ),
        'location' => array(
            'title' => 'Where are you located? (city & country)',
            'value' => '',
            'type' => 'text'
        ),
        'companies' => array(
            'title' => 'Who are the companies and teams behind the video?',
            'value' => '',
            'type' => 'text',
            'multipleLines' => true
        ),
        'businessEmail' => array(
            'title' => 'Is there an email we can contact for business enquiries?',
            'value' => '',
            'type' => 'email'
        )
    ),
    // This is the actual structure of the form as it is displayed, grouped into subsections.
    'structure' => array(
        array(
            'id' => 'videoInfo',
            'entries' => array('videoLink')
        ),
        array(
            'id' => 'aboutSubmitter',
            'dependentOn' => 'loggedOut', // Only uses (shows and processes) this if the 'loggedOut' external value is on.
            'alsoProcessIf' => 'loggedIn', // These entries are automatically filled (see 'automaticallyFillEntriesFrom' further down) so process them even if its dependency is off.
            'entries' => array('name', 'email')
        ),
        array(
            'id' => 'connectionQuestion',
            'entries' => array('helpedCreate')
        ),
        array(
            'id' => 'connection',
            'dependentOn' => 'helpedCreate', // Only show this if the 'helpedCreate' checkbox is on.
            'entries' => array('role', 'location', 'companies', 'businessEmail')
        )
    ),
    // Automatically uses external values to fill in entries.
    'automaticallyFillEntriesFrom' => array(
        // Using external value 'loggedInMember', grab the following values:
        'loggedInMember' => array(
            'name' => 'name', // Entry value for 'name' is automatically filled from $externalValues['loggedInMember']['name']
            'email' => 'emailAddress', // Entry value for 'email' is automatically filled from $externalValues['loggedInMember'][emailAddress]
        )
    )
);

Using external values from the server


function exampleExternalValues() { $loggedInMemberInfo = exampleLoggedInMemberInfo(); $specialValues = array( 'loggedIn' => !empty($loggedInMemberInfo), 'loggedOut' => empty($loggedInMemberInfo), 'loggedInMember' => $loggedInMemberInfo ); return $specialValues; }

Validating & processing

// Check if form is being submitted and if so, process the submitted entries.
$resultsForSubmitVideo = perforatedFormCheckAndProcess($formOptions, array(
    'externalValues' => 'exampleExternalValues' // Options and callbacks are kept separate to enabled $formOptions to be created as pure JSON.
));

Working with submitted results

?>


Video Submissions

Thanks = glazeText($processedEntries['name']['value']) ?> for submitting your video!

``` ## Displaying the form ```php
<?php

TODO

  • Client-side JavaScript validation, probably tie in with an existing project instead of writing from scratch.

The Versions

05/05 2015

dev-master

9999999-dev

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  Sources   Download

MIT

The Requires

 

json form validate forms dependency perforated perforate

21/07 2014

1.5.3

1.5.3.0

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  Sources   Download

MIT

The Requires

 

json form validate forms dependency perforated perforate

27/06 2014

1.5.2

1.5.2.0

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  Sources   Download

MIT

The Requires

 

json form validate forms dependency perforated perforate

14/06 2014

1.5.1

1.5.1.0

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

json form validate forms dependency perforated perforate

23/05 2014

1.5

1.5.0.0

Simple yet pretty powerful forms using associated arrays or JSON, with automatic server validation, dependencies and more.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

json form validate forms dependency perforated perforate