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:
1 |
$form = NibbleForm::getInstance('/location.php'); |
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:
1 2 3 4 5 6 7 |
$form->username = new Text('Please enter your username'); $form->checkbox = new Checkbox('Please select one of the following', array( 'Choice one', 'Choice two', 'Choice three', 'Choice four' )); |
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:
1 2 3 4 |
//Created the field $form->password = new Password('Please enter your password'); //Now add the confirmation with label $form->password->addConfirmation('Please confirm your password'); |
Once the form has all of the desired fields created, it can be rendered to the page:
1 |
echo $form->render(); |
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):
1 2 3 4 5 6 7 8 |
//Make sure the form is submitted if(isset($_POST['submit'])){ if($form->validate()){ //Sweet forms valid do actions required } else { //Oh no, invalid, probably need to re-send the form to the user } } |
Instantiating the form
When the form is instantiated there are a number of arguments that can be provided:
- 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(‘/’);
- Button message: This is another string string that holds value to be displayed on the submit button.
- 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.
- 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.
- Error type: This is a string that can be;
- False: No error messages will be shown
- “list”: Errors will be displayed above the form in a list
- “inline”: Error will be displayed next to the form element they relate to
- “flash”: Errors will be displayed using JQuery notifications, however the Nibble flash class is needed to use this method
- No matter what the selected method, all labels relating to a field with an error will have a CSS class of “error”.
- Format: A string of either “list” or “table” that tells the form class what kind of HTML structure to render the form in.
- 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:
1 2 |
//Instantiate form with inline errors in a table $form = NibbleForm::getInstance('/admin', 'Submit this form','post',true,'inline','table'); |