This is an old revision of the document!
Table of Contents
Form class
The form class takes care of html form input, by both rendering forms and setting/receiving form input.
Form rendering
A form is rendered by first configuring the form object and then rendering it.
$form = new Form('form_id'); $form->addField(new FieldText('Type something', 'field_title')); $form->addField(new FieldSubmit('Save form', 'save_form')); $form->render();
This will render a form with a text input field labeled Type something and a submit button labeled Save form
Obtaining form data
Validation of the form and retrieval of form data, also requires that the form is setup and then validated. As it is important that the form is set up exactly the same way as the posted form, usually the same object is used, and as an example we can type everything in a single example:
$form = new Form('form_id'); $form->addField(new FieldText('Type something', 'field_title')); $form->addField(new FieldSubmit('Save form', 'save_form')); if ($form->isSubmitted()) { // The form was submitted. if ($form->validate()) { // Form input was validated. $values = $form->getValues(); // $values now contain all form values. // Do something when form was posted with success. } else { // When the form didn't validate, it have already been prepared with proper error messages, // so we run-through down to displaying it again where it will also contain the entered values. } } else { // Values can be pre-added to the form if desired $form->setValues( array( 'field_title' => 'Pretyped value'; ) ); } $form->render();
Easy forms
In order to provide an easier way to design forms, one can prepare a special html file
- testform.frm
<h2>Please fill this form</h2> <text label="Type something" name="field_title"> <submit label="Save form" name="save_form">
This file can be used like
$form = new Form('form_id', 'testform.frm');
which will actually prove the same form as above.