Jump to content
xisto Community
Sign in to follow this  

Yet Another Form Generator Written in PHP

Recommended Posts

Looking to revive an old project of mine, i have decided to start from scratch with a different way of approaching the matter. I have decided to make it part of my content management system to ease the process of updating it for publicity's sake and to make my CMS seem more complete. It is written in PHP and requires at least PHP version 5.2 (which isn't a problem for us Xisto-hosted members :)). This project is still a bit immature, but most of it has been tested and seems suitable for many practical situations.


Most of what you would normally find in an HTML form have been turned into their own PHP classes (see here). This has made the process easier to create other fields, and it should allow for other kinds of inheritance. Generating the HTML forms themselves have been left to their own classes (see here). The abstract class, AbstractHtmlForm, should be the base class used when making your own form classes. Some default classes have been provided for convenience and as a blueprint on how to go about in sub-classing the abstract class.


Some things should be noted about this project: For starters, by default it makes use of an HTML 5 attribute, the required attribute (which some browsers, at least Opera, have already implemented). This will cause current XHTML compliant pages to not validate. To avoid this scenario, when instantiating a new field make sure to pass the boolean false to the field's public method, showRequiredAttribute():

Secondly, for users who are XHTML Strict, when instantiating a new form the name of the form is passed in the constructor of the form. XHTML Strict does not contain any name attribute for forms, therefore XHTML Strict pages will not validate. To avoid this you can pass NULL to the name parameter of the constructor and later set the form's ID using the public method, setFormId() (if you want it to have an ID), or you can pass in an actual name to the constructor (to perhaps be used internally for setFormId()) but later pass NULL or an empty string to the public method, setFormName().


Note: you must set those things before calling renderHtmlForm() on the form object.


Thirdly and lastly, one of the reasons for its current immaturity (as of revision 66) includes the fact that the select HTML element does not yet have its own class. Therefore drop down menus and multiple select fields have not been currently implemented. Also, while check boxes and radio buttons have been implemented, they have not been tested or used in any working example. And the FileUploadForm currently has trouble uploading multiple files—though this appears to be more of a browser issue and not something inherent to my script.


Example usages can be found at the end of the file, formtypes.formgenerator.php. You may look at the convenient form classes to know how to implement the abstract class, AbstractHtmlForm, and to get a better understanding of the system.


Future plans for the project include implementing JavaScript related attributes (e.g. onmouseover, et cetera), implementing any missing elements, and assuring stability.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.