Hello,
I had some time and continued to work on an approach for creating forms. Several ideas and code used are from Vincent's replies in my first thread about this. Additionally, it also uses the functionality of his ECLIPSE class libary.
The idea of Filters mentioned in the thread about PEAR::QuickForm made me move the error checking from each field to separate filter classes.
I currently have the following structure:
A class Form which holds references to FormComponents in an array.
FormComponent is the superclass for all fields. Subclasses are TextField, TextArea and PasswordField.
Each field has a basic filter that checks if input is required for the field or not. Subclasses of this Filter are for example EmailFilter which checks the validity of an email adress and can be applied for a field.
Currently, a FormComponent can only use one filter. However, I think that this is sufficient.
Here is an example for an AddressForm:
I have attached the source files in order to run the example above. However, the source files are not properly commented (ie. there are hardly any comments at all ;-) )Code:<?php if (!defined('CLASSROOT')) { define ('CLASSROOT', 'classes'); } require_once CLASSROOT.'/form/Form.php'; require_once CLASSROOT.'/form/FieldFactory.php'; require_once CLASSROOT.'/form/SampleViewer.php'; require_once CLASSROOT.'/form/Filter.php'; class AddressForm extends Form { function AddressForm($method, $action) { parent::Form($method, $action); // name type needed filter $this->addField('firstname', 'text', true, new Filter()); $this->addField('lastname', 'text', true, new Filter()); $this->addField('address', 'text', true, new Filter()); $this->addField('telephone', 'text', false, new TelephoneFilter()); $this->addField('email', 'text', false, new EmailFilter()); } } $form =& new AddressForm('post', $_SERVER['PHP_SELF']); $form->display(new SampleViewer, $_POST); if (!$form->submit($_POST)) { echo '<pre>'; echo $form->getErrorMessage(); echo '</pre>'; } else { echo 'success'; } ?>
If you would like to try it out, I think it is best if you extract the files to <apachedir>/htdocs/classes/form. Otherwise the classroot constant in those files needs to be changed. As I use ECLIPSE you would also need it.
I used Windows, so I don't know if it runs under Linux as well.
Thanks in advance for any comments,
Christian





Bookmarks