Fork me on GitHub

Nibble Development

Nibble Development

PHP classes and libraries for developers

Nibble forms, PHP/HTML form generation and validation class, v1.3.0

What is Nibble forms?

Nibble forms is a stand alone PHP class collection that allows developers to begin easily developing HTML forms with PHP validation. The forms that can be developed are very customisable, however they can also be set up with minimal arguments. The documentation below shows how to implement the forms from simple one argument forms to fully customised forms. Nibble forms is now on github, check out the git repository.

Simple overview

The simplest form can be set up with just one argument, in one line of PHP, the target of the form:

The simplest of form fields can be created with just one or (if the field needs options like a radio/checkbox field) two arguments. Options are provided using PHP arrays:

Nibble forms also have some simple features for confirmation fields such as “Please confirm your password”, a field for the user to repeat the password they entered to make sure it is what they want it to be. All that is required is a simple PHP call to the “addConfirmation” method:

Once the form has all of the desired fields created, it can be rendered to the page:

Validation is also a simple process, Nibble forms has a validate function that returns true or false depending on whether the form validates. If the form does not validate the errors are also rendered with the form in the manner assigned in the form instantiation (more on error rendering in the Instantiating the form section):

Instantiating the form

When the form is instantiated there are a number of arguments that can be provided:

  1. Location/target: This is a string that holds the location or target for the form. This argument is required and there is no default.┬áNibbleForm::getInstance(‘/’);
  2. Button message: This is another string string that holds value to be displayed on the submit button.
  3. Method: The method is a string, either “get” or “post” that tells the class what method to apply to the form. Note that the validation is built around the post method.
  4. Sticky: True or False, True tells the form to remember what the user entered and to fill in the values of the form upon returning it to the user, False means the form will be returned empty on each submit.
  5. Error type: This is a string that can be;
  6. False: No error messages will be shown
    1. “list”: Errors will be displayed above the form in a list
    2. “inline”: Error will be displayed next to the form element they relate to
    3. “flash”: Errors will be displayed using JQuery notifications, however the Nibble flash class is needed to use this method
  7. No matter what the selected method, all labels relating to a field with an error will have a CSS class of “error”.
  8. Format: A string of either “list” or “table” that tells the form class what kind of HTML structure to render the form in.
  9. Multiple errors: Either True or False, this argument sets the number of errors to be displayed for each form field. True will display every error for each form field, False will display only one error.

Below is an example of a fully customised form instantiation:

Luke Rotherfield

Freelance PHP Developer
is a PHP developer dedicated to writing the most awesome liraries for his fellow devs :) enjoy

Latest posts by Luke Rotherfield (see all)

Leave a Comment