Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Easier form validation with PHP

Let’s talk about form validation. Here’s what I would class as the ideal validation system for a form in a web application:

  1. The form is displayed; you fill it in.
  2. You submit the form to the server.
  3. If you missed something out or provided invalid input, the form is redisplayed pre-filled with the valid data you already entered.
  4. The redisplayed form tells you what you got wrong. It also flags the fields that were incorrect.
  5. Loop until you fill the form in correctly.

Writing this once in PHP is trivial, but takes quite a bit of very dull code. Writing this for more than one form quickly becomes a tedious nightmare of duplicating and slightly editing code, which is why so few forms bother.

I’ve been trying to figure out an elegant way of automating as much of this code as possible on and off for more than two years. I’ve tried systems that generate the whole form based on a bunch of criteria (too inflexible), systems that describe the validation rules (fine but they don’t help with the redisplay logic) and I’ve looked at solutions available in other language (such as ASP.NET), all to no avail. Over the past couple of days I’ve been working on the problem again, and for the first time I think I’m actually getting somewhere.

My latest attempt (sparked by this article on Evolt) involves embedding validation and redisplay rules in the markup of the form itself. The form is written in XHTML, but with a number of additional tags and elements. Any form field elements can have a number of additional attributes which specify the validation rules of the form. For example:


<input type="text" name="name" 
  compulsory="yes" validate="alpha" callback="uniqueName" 
  size="20" />

The yellow highlighting in the above line marks the additional elements. In this case, they mean that the field is compulsory, must contain only letters, and should be checked for final validity by calling the user defined uniqueName() function.

There are a few other validation attributes, but the above gives a good idea of how the system works. The second problem is how to display errors. Part of the solution here is my custom <error> element, which can be used to associate an error message with an error in a particular field. In my tests I’ve been using this to display an exclamation mark next to invalid fields:


<input type="text" name="name" 
  compulsory="yes" validate="alpha" callback="uniqueName" 
  size="20" />
<error for="name"> !</error>

This associates the text inside the <error> element (in this case the single exclamation mark) with the “name” field.

So far, all I’ve done is add a bunch of invalid markup to an otherwise valid chunk of XHTML. The key is how this markup is processed. FormML (for want of a better name) is never passed to the browser; instead it is processed by my PHP FormProcessor class before being displayed. This class strips out all of the FormML tags, and also applies logic to the rest of the form based on the information from the tags. Because the whole thing is XHTML, this can be relatively easily achieved using PHP’s built in XML parser. The XML is modified on the fly to create the XHTML that is sent to the browser. In the above example, the contents of the <error> element would only be displayed if the form was being redisplayed and the user had not filled in their name correctly. In addition, the class populates the value attribute of each input element with the previously entered data and adds an “invalid” class to the element to allow it to be styled appropriately.

The next problem is to display a list of descriptive error messages describing the problem. This took slightly longer to work out: I needed a way of setting an error message for each potential problem (remember there are several ways a field can be invalid: it could have been left unfilled, or it could have failed a regular expression check, or it could have failed a callback function), and I also needed some way of indicating how these errors should be displayed. My eventual solution was to introduce a new <errormsg> element for specifying error messages, and a simple templating system (based on a few more custom tags) for indicating where these errors should be displayed. I’m not entirely happy with the way this works at the moment, but here’s what I’m using:


<errorlist>
<ul>
<erroritem> <li><message /></li></erroritem>
</ul>
</errorlist>
<errormsg field="name" test="compulsory">You did not enter your name</errormsg>
<errormsg field="name" test="alpha">Your name must consist <em>only</em> of letters</errormsg>

The <errormsg> elements can be placed anywhere in the markup, and will be removed before display. The <errorlist> fragment defines the template for the list of error messages, with the <erroritem> element enclosing the “template” for each error and the <message> element showing where the actual error message (as defined elsewhere) should be displayed. Note that the <errormsg> elements specify the field and the test that they relate to. It is not necessary to provide custom error messages for every possible field/test combination; the system generates moderately intelligent error messages in the event that one has not been provided.

That’s a lot of custom markup to define the behaviour of a form. The good news is that the actual PHP used to display, validate and redisplay the form is incredibly simple. Here it is:


$formML = '... formML XML code goes here ...';
$processor = new FormProcessor($formML);
if ($processor->validate()) {
    // The form has been submitted and is valid - process the data
    echo 'Form data is OK!';
} else {
    // This will display the form for the first time OR redisplay it
    // with relevant error messages
    $processor->display();
}

And that’s all there is to it.

The code is still under very heavy development. At the moment it’s messy, has several minor bugs (and possibly some major ones I haven’t yet uncovered), isn’t fully tested and is almost certainly not ready for deployment. Never-the-less, you can play with a demo form that uses it or grab the code here:

Incidentally, I haven’t mentioned javascript in the above in an attempt to keep things simple. I know client side validation is a great addition to stuff like this (provided it’s backed up by solid server side logic) and one of my longer term aims is to dynamically add the necessary javascript to the form during the processing phase, thus skipping the need to write any boring validation code by hand.

This is Easier form validation with PHP by Simon Willison, posted on 17th June 2003.

View blog reactions

Next: Gorgeous CSS Rollovers

Previous: HTML Definition Lists

151 comments

  1. Very nice Simon, I wish I had your motivation to actually get on and tackle projects like this. Combine it with a script which will automatically create XHTML code for forms as well (they are so tedious to code) and you're onto a winner! :) Btw, why do you no longer hang in #webdev on Quakenet?

    Smiler - 18th June 2003 01:16 - #

  2. I've been avoiding IRC completely for about a year because I kept on losing whole evening just chatting. I'm thinking about starting the habit again soon under controlled circumstances (limiting myself to a few channels) though.

    I actually wrote something like this that handled automatic generation of the XHTML ages ago, but in practise it wasn't flexible enough to be useful. I ended up dropping it completely because it wouldn't let me tweak the form layotu easily enough.

    Simon Willison - 18th June 2003 01:18 - #

  3. Nice job dude. Nice job. Also see the strategy pattern from http://www.phppatterns.com/ Scott

    Scott Johnson - 18th June 2003 01:44 - #

  4. 99/60/0000 is aloud ;)

    Anne van Kesteren - 18th June 2003 06:59 - #

  5. Like the approach. Basically that's very ASP.NET-like; the "template" for the form contains markup which tells the underlying classes how to behave. Couple of tip offs; don't know if you knew that saving PHP source code with a .phps extension makes it available for viewing (plus has pretty colors!). Also if you're looking of a parser capable of HTML (badly formed XML), submitted one to PEAR here: http://pear.php.net/package-info.php?pacid=203 - it's got more or less the same API as the native PHP SAX parser so you could probably just override the constructor for FormRuleExtractor in a subclass. Some other things that may be worth considering; - make the classes independent of the markup (i.e. users can defined their own markup) - this might help your code fit into other projects. - make the validation rules easily extensible, so there's no need to modify the FormRuleExtrator to add new rules. The strategy pattern example could be one way to do it. - something that could improve peformance would be to "compile" the formML into native PHP and cache it Anyway - just ideas (i.e. more work!). Great job.

    Harry Fuecks - 18th June 2003 10:22 - #

  6. Very cool idea. The date validation needs a little work: although it gets form right, it doesn't check ranges properly. This would be pretty trivial to fix.

    GaryF - 18th June 2003 15:25 - #

  7. Yeah, the date range thing isn't meant as a proper example of validation, it was just the first thing that came to mind when I wanted an example of something you could use a regexp for. To properly check it could write a checkDate function and use callback="checkDate" as an additional attribute on the input element.

    Simon Willison - 18th June 2003 16:02 - #

  8. Very cool... I've wanted something like this for a very long time, I just couldn't think of a good way to do it. I've found that building forms (actually, layout in general, it's just that forms are almost always different from each other) is the most dull thing about doing web development (with PHP, at least).

    Manuzhai - 18th June 2003 18:06 - #

  9. This post has come in the middle of my thinking about what is the best templating methodology.. I've been looking at xml style, attribute addition (as yours is) new markup (smarty style) etc.

    I've written a template pre-parser that you might be interested in (takes html or xhtml and applies various transformations and extracts sections etc seems to cover nearly all possibilities I could think of.

    Anyways, I have no real conclusions. I want a templating language that is easy to learn and yet provides a smooth learning curve to power user. I like tal style templates. I also like albatross. I think a template language should offer the sorts of functionality you demonstrate here. I also am incredibly frustrated with smarty. Flexy seems good but ruins my HTML.

    My path to creating a new template language has reached this postulating ideals point (it seems a classically, and admirably, lengthy point in python developent in particular). I want to get this right so am soliciting as much feedback as possible. any thoughts?

    Tim Parkin - 18th June 2003 21:19 - #

  10. Simply brilliant, Simon. Can't wait to see the final product!

    Bob - 19th June 2003 04:01 - #

  11. But doesn't this mean you are putting your business logic in your template, which I assume also contains most of the presentation logic. Wouldn't that be a bad idea, ie. the programmer and the designer would have to work on the same file? Nice idea though, if it works you will have solved one of the ongoing annoyances with PHP development.

    PeterV - 19th June 2003 14:39 - #

  12. Hmm.. it does blur the line between presentation and logic a bit doesn't it? Then again, the business logic of an application usually deals with more interesting / important things than just ensuring that input is of the correct format, so I think it's OK to move validation to the presentation side. I know for certain that the kind of applications I develop will definitely benefit from form validation being handled in this way.

    Simon Willison - 19th June 2003 16:26 - #

  13. As Harry already said, this is very close to what ASP.NET does. You may want to have a quick look at how MS does it in ASP.NET, as it may save you work in terms of figuring out what works :) J

    Jeremy C. Wright - 19th June 2003 23:11 - #

  14. I did write a very long post on this subject and about logic/presentation, but as addcomment.php just gave me no reply I've lost the whole b****y thing. So I will just say: have a look at PHPTal (use google - I'm sick of typing it) and integration between the two would be good.

    Peter - 20th June 2003 09:51 - #

  15. You could also extract an SQL schema from the XHTML, to generate the initial database. So the schema "source code", form, rules, everything would be in one place!!!!!!

    mitchell perilstein - 23rd June 2003 18:58 - #

  16. XForms answers this problem. An XML Schema is used that can validate data input on the client side, before being uploaded and run against a schema on the server as a seccurity check.

    The php idea above is a nice idea, but it's reinventing the wheel in many ways.

    Ben Nolan - 24th June 2003 03:15 - #

  17. I agree totally with the problem of auto generated forms and the hard to tweak issue, and generating the bulk of the code then tweaking with no way for the definition to take on board the tweaks is next to useless. I have fundamental issues of specifying validation guideliness in such a vunerable manner, I think its a nice idea in an ideal world, but is this not open to abuse? I doubt many would entrust validation purely to javascript or other client side methods (as you commented on), wouldn't this method have the same pitfalls?

    dibby - 24th June 2003 15:44 - #

  18. I just took a look at XForms, and on first glance it's just too different from HTML forms to be particularly useful for my purposes. I want to build something that developers with knowledge of HTML forms can use without having to learn a whole new markup language, so keeping everything as close to HTML as possible has major benefits. I'm definitely going to add a namespace to distinguish form validation elements and attributes from HTML ones, mainly to improve readability and make it more obvious which bit is doing what.

    Simon Willison - 24th June 2003 17:59 - #

  19. I too thought that this is reinventing the wheel re: XForms, but having developed something similar for myself, I found it to be far superior because (in my world, at least) there are virtually no XForms clients (browsers that handle XForms properly) whereas with this system, it's just HTML (or XHTML). Also, this is actually a complete abstraction of db, logic, presentation. Since the base form is stored in a separate file, the designer can design it however he/she would like it to look, including the look and feel when the user fails to complete the form properly (this is really huge as more often than not, it is left to the programmer to design the display of error messages, and the copy, and that's largely why they are so incomprehensible). The developer can write a single PHP (or any server-side code, for that matter) file that displays the form, processes the input, and redirects the user as needed without having to comingle PHP and HTML. Very nice. Good luck!

    Ted Stresen-Reuter - 27th June 2003 16:21 - #

  20. Call me a Luddite, but... I just don't find making the forms and validating the content that big a deal in the first place, and I've never ever found a validator, much less a form-generator, that is flexible enough to handle my real-world requirements. I'll end up writing so many special case FormML code that I might was well just code from scratch.

    Richard Lynch - 27th June 2003 19:40 - #

  21. I was wondering if anybody tried to use a WYSIWYG XML Editor like BitfluxEditor to create and edit such forms in an easy WYSIWYG way. The possibility of creating forms online might be a cool feature for content management systems... one would just need to add some automatic backend creation, for example a corresponding database table et voilà! Users would have the possibility to create custom forms for their websites just as easy as they can publish content. I believe this is something i will investigate as soon as i find some time :-)

    Jean-Marc König - 2nd July 2003 17:14 - #

  22. Great stuff. I love how this thing works. Just a quick question, what licence (if any) is this code released under? Is it public domain? GNU? BSD?

    Dark_Greg - 3rd July 2003 07:41 - #

  23. Let's say BSD - I'm happy for anyone to use it, I'd prefer if people tell me what they've done with it and mail me any suggestions / fixes but it's basically free for anyone who wants it.

    Simon Willison - 3rd July 2003 08:20 - #

  24. BSD is great..means i can still charge money for stuff I use it in :) Incase you are wondering, First project I'll be using it on for serious use will be a user system and file upload system. A field for checking if a file is a valid filetype...or general handling for files in forms would be nice. Its doable with the callback function I know, but would be nice to have native support for it.

    Dark_Greg - 3rd July 2003 08:59 - #

  25. Hmm... I still haven't got around to adding support for checkboxes, radio boxes and select fields. Select field support would be particularly useful - the validation part needs to ensure that the value sent from the form is available as an option, to avoid potential abuse from users. I'll try to do some more work on the class in the near future, it's still very much a working prototype.

    Simon Willison - 3rd July 2003 09:05 - #

  26. Interesting idea, but I think it needs some sort validation mechanism to ensure that the form you are receiving is indeed what you expect. Unless I missed something, there is no way to prevent someone from submitting their own modified version of a form and submitting it to a remote url: <form action="http://www.notmyserver.com/signup.php" method="post"> <input type="text" name="name" id="name" compulsory="NO" validate="alpha" callback="uniqueName" size="20" /> <input type="text" name="email" id="email" compulsory="YES" validate="ALPHA" size="20" /> <input type="submit"> </form> If such a form where accepted, you'd accept input where the name field is not mandatory, and the email field accepts any alpha-num input.

    lhb - 5th July 2003 10:16 - #

  27. lhb - that wouldn't work because the HTML of the form is irrelevant; the only thing that matters is the form as stored inside your application. This internal representation has all of its sepcial tags and attributse removed before it is sent to the user. If someone else where to build their own HTML form with specialist attributes the PHP application would never even notice, as it would only see the data sent by the form and not the actual markup used to create it.

    Simon Willison - 5th July 2003 11:50 - #

  28. Gotcha.. I should've looked at the actual output :)

    lhb - 5th July 2003 12:14 - #

  29. just a bit of a question, is it possible to use an object for a callback? something like callback="$user->uniqueName"...i havnt tried it yet...just wondering if it'd work...

    Dark_Greg - 9th July 2003 03:52 - #

  30. Greg: Not yet, but that's definitely something that could be hacked in with a bit of work.

    Simon Willison - 9th July 2003 10:14 - #

  31. ok heres an idea...can functions from classes be called like this?:

    callback="user::uniqueName"

    yeah? that should work shouldnt it? (again untested, i'll be giving this class a SOLID workout over the weekend...and can post detailed experiances if you like)

    Dark_Greg - 9th July 2003 12:10 - #

  32. zazaz

    zaz - 31st July 2003 10:22 - #

  33. I've posted some modified and extended code based on the above on my site. Find out more details

    Peter Bowyer - 4th August 2003 20:48 - #

  34. I really like this! I've played around with this script a bit and used it to create this form: http://www.infoalli.com/requestbid.php. I made a few modifications of my own in order to handle radio buttons and check boxes. I also modified the constructor to be able to accept a file name or a string containing FML. This saves a bit of typing, so you don't have to write the code to read the file into a string for every form you write.

    I'm also evaluating the possibility of using it in some software I'm developing. I'll keep you updated on any other modifications I make and how I use it.

    Thanks!

    Wayne Jensen - 7th August 2003 07:21 - #

  35. testing php scipt

    ikke - 9th August 2003 15:33 - #

  36. Hi, Thanks very much for taking the time to develop this. I've been trying to implement it on a page I'm building. Everything seems to be working fine, except for the callback function, which I guess I don't understand. I want to validate a code against a table. I set a callback="uniqueCode" then write the uniqueCode function. However it never gets called. I also tried putting the code into "uniqueName" but that doesn't work either. In fact, if I change the test form to have uniqueName return false it has no effect on the form validation. Any idea what I'm missing? thanks Guy

    Guy - 28th August 2003 08:02 - #

  37. Simon - Thanks a ton for the work on this awesome form validation code. You have helped me more than I can explain. I have a page I have been working for a while and my problem has been solved. Keep up the good work. I have not encountered any bugs so far. I will let you know if I do. Have a great week! - Forbes

    Forbes - 16th September 2003 02:10 - #

  38. Thanks, for your work that helped me a lot~ by the way.. i was testing this code and found out that i can't get my pre-defined error messages(on test.php) for validations up on the screen. Why is that? i get those pre-defined messages for compulsary checks but not for validations,, can you please look into it and tell me why? or fix it for me so i could see those pre-defined error messages? Thanks.

    Alex - 8th October 2003 23:59 - #

  39. I'm not actually maintaining this code any more. However, Peter Bowyer has taken on my code and rewritten it from the ground up to do even more without any of the bugs. He posted about his class here, and his blog also contains other posts about it.

    Simon Willison - 9th October 2003 00:26 - #

  40. Hey, I think this class is very cool... but when I put in a dropdownbox I get... XML error: not well-formed at line 10 It's a select statement, and when have one item selected it doesn't work. When I take the select out it works :( //// option selected value="x"

    paulh - 10th October 2003 13:46 - #

  41. paulh - I had this problem too. change your input tag to have [selected="selected"] in it (no brackets) and it should work. Thank you, Simon, for developing this code. It is just what I've been looking for. I'm a relative newbie to PHP and XML though, so hopefully this question is as stupid as I think it is. I've got my form validating great, but I once it's valid, how do I actually submit it? I want the data to go into a sql database, and I've created a PHP function with the insert code, I just don't know how to invoke it after the "if ($processor->validate()) {" line. Any words of advice from anyone? Thanks.

    daniel - 12th October 2003 02:26 - #

  42. Daniel, $_POST

    paulh - 12th October 2003 03:04 - #

  43. is an associative array, just pull your values out based on the "name" value in the form :)

    paulh - 12th October 2003 03:06 - #

  44. Hail to the King!!!

    Chris - 2nd November 2003 22:39 - #

  45. I know this topic is long out of date (only found this site today), but I just wanted to note that (X)HTML forms already have business logic in them. Forms aren't the usual clear separation of content and style — they also include attributes like maxlength. It's my experience that forms will always have blurred lines between business and template logic, unless you let go of the reigns.

    Justin French - 8th December 2003 03:13 - #

  46. hgh

    h - 7th January 2004 21:26 - #

  47. test

    444545545 - 8th January 2004 13:56 - #

  48. Hello, This topic is old but I just got to it today.Can anyone tell me how use this class n my projects?

    wiredX - 30th January 2004 12:07 - #

  49. Ran across this post a few days ago while searching for something related. Specifically, I was looking for a way to get custom tags in PHP, similar to what you have when coding JSP. Custom tags allow you to define new tags that when encountered in mark up are transformed into something else. You do something similar here with your FormML, but with FormML the tags are "hard wired". With custom tags the developer can create new tags by writing a "tag handler", and thus can extend the available tags easily.

    While searching for a custom tag solution I ran across the Pear XML_Transformer class, which would do precisely what I want, but like most things in Pear, it's basically not documented and appears to not have a stable enough code base for me to feel safe using the source as documentation. Not to mention it's a little to over designed (i.e. there's a lot of functionality you'll rarely use that you're burdened with even when you don't want it). I also found this, which gave a nice starting point for what I want.

    So, why is this a relevant comment? I've got code mostly implemented that allows you handle forms similar to what you're doing, though I place the validation in the PHP file, not the "template". Custom tags in the template take care of populating the form, so <html:input name="FieldName" type="text" /> automatically populates with the content of $_POST["FieldName"]. Error messages are displayed in a fashion similar to what you've done, but more inline with how things are done in Apache Struts. The end result is similar to yours, but with a cleaner separation of presentation and business logic and a more extendable template markup language. The end result I hope will be a PHP library that supplies most of the functionality available in Struts (including Tiles). If you're interested I'll keep you informed of my progress.

    wekempf - 9th February 2004 17:52 - #

  50. jtj egeg

    egege - 10th February 2004 15:20 - #

  51. you are cool

    carolina - 10th February 2004 22:36 - #

  52. I love you and also think me and you were meant for each other

    Laura Lara - 17th February 2004 00:50 - #

  53. I like to party

    Azizus Salehin - 29th February 2004 06:03 - #

  54. if an email field is set compulsory="no" and the field is left empty, it will be displayed as invalid although it was just empty.

    Sergio B. - 23rd March 2004 00:25 - #

  55. cvb

    bcv - 4th April 2004 16:15 - #

  56. htdyg

    6666 - 12th April 2004 13:56 - #

  57. ghkfgk

    fuj - 14th April 2004 13:28 - #

  58. Hi Simon, your FormProcessor class is very useful. I just had a few questions/notes. Sometimes you use $my_array[myname] instead of $my_array['myname']. The second question is as follows: The regular expressions you specified for matching alpha and for numeric strings seem to be the same. With my beginning knowledge of regular expressions, it seems that the expression for numeric would be something different. The PHP function is_numeric could also be used ...

    Jay Sheth - 20th April 2004 17:45 - #

  59. sdf

    df - 6th May 2004 03:53 - #

  60. hmmmm asian chicks ^_^ nice work dude :D

    Intosia - 14th May 2004 08:08 - #

  61. dhs

    tywr - 30th May 2004 06:52 - #

  62. rewr

    werewr - 21st June 2004 23:33 - #

  63. dfgfdgf

    fdshdfgh - 2nd July 2004 17:46 - #

  64. zszsfz

    zfdzs - 15th July 2004 09:23 - #

  65. kl;l;lk

    aSAsasS - 27th July 2004 08:13 - #

  66. yutyuyt

    red - 30th July 2004 00:51 - #

  67. Simon, Nice solution, I have a question for you , taking this a step further do you know of any good form generators processors that support insert/update/delete function, just like validation coding these operations into a form/php are tedious, just curious.

    tony - 1st October 2004 01:45 - #

  68. helo helo heloooooo...

    helo - 7th October 2004 04:56 - #

  69. rthrthtrh

    rtrh - 10th October 2004 23:59 - #

  70. asdf

    asdf - 18th November 2004 10:02 - #

  71. Dear Sir,

    Thanks so much for this nice article. I was wondering if some one has allready done work on Form Validation and automation in PHP and i found exactly what i was searching. Your article is very informative. May be some one can take up from here and make it more useful and release it as a GPL Code for automation of HTML Forms.

    Thanks for sharing.

    Chantu

    http://www.chantu.com

    PS: humm ... i wonder why you dont allow capital letters in Tags :D

    Chantu - 19th November 2004 20:59 - #

  72. So i was wondering, all of the above in mind...that i dont have a clue about PHP XML or XHTML, is there any hope for me at all for someone to come along and develop this sort of thing and finish it for 'deployment' ? :D maybe if some1 has any ideas feel free to mail me. Id be delighted !

    Jamie - 10th December 2004 15:35 - #

  73. hi

    matt - 15th December 2004 17:57 - #

  74. asd

    fasdf - 29th December 2004 14:40 - #

  75. eree

    tewe - 9th January 2005 05:49 - #

  76. xcv

    vx - 14th January 2005 20:44 - #

  77. asdfasdf

    asdfasdf - 20th January 2005 16:34 - #

  78. At work we have actually done something simlar and created templates to be built around the code. We use definitions in arrays to define recordsets, database tables and form fields, using classes extensively. We plan to open source the code in the next few months, but we have no time for it as we're running one man short until mid april. Here's a definition I've picked semi-randomly
    		'Users'=>array(
    			'Editable'			=> '1',
    			'AllowNew'			=> '1',
    			'AllowEdit'			=> '1',
    			'AllowDelete'		=> '1',
    			'EditTable'			=> array('RoleType'=>'Admin','Location'=>'Local'), //must be admin over /admin folder
    			'ListTable'			=> array('RoleType'=>'Admin','Location'=>'Local'), //alternative is 'Anywhere'
    			'Type'=>'Users',
    			'Related'			=> array(array(	'Caption'				=>'Roles',
    												'RelatedTable'			=>'roles',
    												'RelatedField'			=>'roleid',
    												'RelatedDisplayField'	=>'roledesc',
    												'LinkerTable'			=>'rolemembers',
    												'LinkerRelatedFields'	=>array('roleid'),
    												'LinkerThisFields'		=>array('username')
    											)
    										),
    			'Fields'=>array(
    				'username'		=>array('Type'=>'Text', 'Size'=>'40', 'Create'=>'VARCHAR(40)', 'ListPos'=>'1', 'SearchBy'=>'=', 'Validation'=>'NotBlank','PriKey'=>'1', 'Caption'=>'User name', 'ToUpperCase'=>'1'),
    				'firstname'		=>array('Type'=>'Text', 'Size'=>'30', 'Create'=>'VARCHAR(30)', 'ListPos'=>'2', 'SearchBy'=>'=', 'Validation'=>'NotBlank', 'Caption'=>'First name'),
    				'lastname'		=>array('Type'=>'Text', 'Size'=>'60', 'Create'=>'VARCHAR(60)', 'ListPos'=>'3', 'SearchBy'=>'=', 'Validation'=>'NotBlank', 'Caption'=>'Surname'),
    				'active'		=>array('Type'=>'Boolean', 'WidgetType'=>'Checkbox', 'Create'=>'BOOLEAN', 'ListPos'=>'4', 'Caption'=>'Is the account active?'),
    				'accountcreated'	=>array('Type'=>'DateTime_Created', 'DisplayAs'=>'readonly', 'Size'=>'120', 'Create'=>'TIMESTAMP',  'Caption'=>'Time account created'),
    				'accountcreatedby'			=>array('Type'=>'Text_CreatedBy', 'DisplayAs'=>'readonly', 'Size'=>'40', 'Create'=>'VARCHAR(40)', 'Caption'=>'Account created by'),
    				'accountmodified'	=>array('Type'=>'DateTime_Updated', 'DisplayAs'=>'readonly', 'Size'=>'120', 'Create'=>'TIMESTAMP',  'Caption'=>'Time account last modified'),
    				'accountmodifiedby'			=>array('Type'=>'Text_UpdatedBy', 'DisplayAs'=>'readonly', 'Size'=>'40', 'Create'=>'VARCHAR(40)',  'Caption'=>'Account modified by'),
    				'email'			=>array('Type'=>'Text', 'Size'=>'80','Caption'=>'Email Address', 'Create'=>'VARCHAR(80)', 'ListPos'=>'5', 'SearchBy'=>'LIKE'),
    				'password'		=>array('Type'=>'Text', 'WidgetType'=>'Password','Size'=>'15','Caption'=>' Password',  'Create'=>'VARCHAR(15)'),
    				//'usertype'			=>array('Type'=>'Integer', 'WidgetType'=>'Select', 'Options'=>$aUserTypesText, 'Create'=>'INT2', 'ListPos'=>'9', 'SearchBy'=>'=', "Caption"=>"Usertype"),
    				)
    			),
    
    Although this is not comprehensive of what is currently being done, it gives you a good idea. We'll probably announce it on freshmeat when we're ready - we'll have to name it as well. It will also be announced at zedcore.com It certainly does make form creation easy, as well as database integration, validation, table creation, maybe one day table editing can be done after a change in the definition.... The array also includes items to guide the database management tool in our CMS.

    richard - 22nd February 2005 19:52 - #

  79. sdfsdfs

    fd - 3rd March 2005 02:58 - #

  80. bvnvbbnb

    bvn - 3rd March 2005 15:30 - #

  81. you state "The form is written in XHTML, but with a number of additional tags and elements"

    correct me if i'm wrong, but strict XHTML validation will fail this markup because of those extraneous attributes/tags.

    jeff - 7th March 2005 19:56 - #

  82. nevermind...didn't read the next paragraph before posting :-O

    jeff - 7th March 2005 19:57 - #

  83. Your email validation is too restrictive – the following could be a perfectly valid email address (as per RFC 2822):

    " spaces! @s! \"escaped quotes!\" "@łódź.pl

    Shot - 7th March 2005 20:33 - #

  84. asdfsd

    sdfsd - 14th April 2005 22:32 - #

  85. Great solution. How would one go about editing the code so that if the data is all validated, it posts the data to another page?

    Justin - 21st April 2005 01:38 - #

  86. ewr

    wer - 26th April 2005 22:14 - #

  87. fdgsdgdf

    gfdsgfd - 17th May 2005 02:17 - #

  88. bdbsbs

    ngdn gdgs - 18th May 2005 19:02 - #

  89. I am currently attempting to implement this code into a form being used at a secondary school to get data from the students. I have the form working with normal text fields, however I am struggling to implement radio buttons, any help or pointers would be greatly appreciated.

    Glen - 20th May 2005 11:52 - #

  90. fsad

    fdsa - 4th June 2005 10:17 - #

  91. hfhf

    hgfh - 7th June 2005 06:05 - #

  92. Greetings. Like many others before me, I too have been looking for the "right" form processing class to use in various projects and came across your and Peter's work. I just wanted to drop a note to say thanks for sharing, and have extended Simon's work more closely to your original direction. Here is a very much still in-progress change log:
    	+ reintegrated original inline ruleset parsing
    	+ added standardized error gylph indication
    	+ added XML translate entities function for more robust handling with default XML parser
    	+ added trim and strlen check to regexp and callback error checking to prevent false positives
    	+ added min and max length error checks
    	* cumpulsory renamed to required
    	* required rule can be any value (yes, y, etc)
    	* alpha regexp validation allows spaces
    	* fixed numeric regexp validation
    	* error messages simplified to 'validate', no longer specific to regexp or callback
    	- PEAR XML parser class
    	- separate XML ruleset parsing
    	- inline error message handling
    
    Let me know if for any reason you would not want this released (I assume the original was GPL but could not confirm this).

    Gestaltware - 7th June 2005 22:10 - #

  93. 232323

    3232 - 25th June 2005 05:40 - #

  94. hhh

    hhh - 25th June 2005 07:34 - #

  95. fff

    fffffffff - 28th June 2005 05:38 - #

  96. fdfd

    fdfd - 7th July 2005 04:23 - #

  97. asdad

    asdasd - 30th August 2005 06:00 - #

  98. fdgfdgdf

    rtr - 6th September 2005 10:03 - #

  99. Câ??est plutôt quâ??tâ??es une grosse fainéante et que ton anglais pue le vandamisme! Eéééééééééàéùà ƒÂ¹ÃƒÂ§ÃƒÂ¹ÃƒÂ Ã¢??éûàéààûà ÃƒÂ©ÃƒÂ Ã¢?? Sorry I'm just trying....

    gilemon - 13th September 2005 13:36 - #

  100. How the hell can I feed xml in french with a form ????

    gilemon - 13th September 2005 13:38 - #

  101. Great Idea !!! I'll try and creat my own version :)) Thank You.

    geza - 20th September 2005 23:39 - #

  102. f

    s - 25th September 2005 16:39 - #

  103. asdf

    asdf - 27th September 2005 04:05 - #

  104. hjkfyko

    gfgjcfgj - 4th October 2005 23:19 - #

  105. I wood like a tree cut down

    Charles Peek - 12th October 2005 22:27 - #

  106. f

    f - 15th October 2005 05:54 - #

  107. tyeryer

    fghgfh - 28th October 2005 09:12 - #

  108. Hi, I am creating a personal website. The user is required to submit his details. I have a problem. Lets say, a user can enter his workplaces. Now a user can have many workplaces. So i read it from database and display it thru a for loop in php. But when i tried using your FormProcessor. But now the problem is, I could use it if i already knew the form. What should i do to use the form processor with a dyanamic form? please help me. its urgent. thank you.

    Wolverine - 16th December 2005 12:44 - #

  109. sdf

    sdf - 26th December 2005 20:20 - #

  110. microsoft

    Joshua - 28th December 2005 23:40 - #

  111. fdgfdgfd

    ffdfdf - 15th January 2006 14:29 - #

  112. vcb

    cvb - 26th January 2006 13:56 - #

  113. vcb

    cvb - 26th January 2006 13:56 - #

  114. Does anyone know how to validate radio buttons, check boxes or dropdown menus?

    Vasil - 1st February 2006 05:50 - #

  115. zxc

    zxc - 7th February 2006 05:18 - #

  116. love this but how do you do i validation on forms for option boxes

    praveen - 7th February 2006 09:57 - #

  117. hello

    mlancvaster - 14th February 2006 14:07 - #

  118. qwerwerw

    jghgjkhgu8i - 16th February 2006 08:46 - #

  119. sdf

    sdaf - 28th February 2006 09:57 - #

  120. I added some code to deal with radiobuttons and checkboxes. In the tag_open function of the class FormProcessor I changed the code that deals with the value of the input field in the $_POST array. I tried to post the code aswell but I cannot form the XHTML well

    Hans van Domselaar - 28th February 2006 13:11 - #

  121. nm,

    68 - 1st March 2006 06:00 - #

  122. yt

    hgj - 1st March 2006 06:01 - #

  123. yt

    hgj - 1st March 2006 06:01 - #

  124. yt

    67 - 1st March 2006 06:01 - #

  125. dfgsfg

    fg - 3rd March 2006 08:50 - #

  126. fdfe

    wfwe - 20th March 2006 13:12 - #

  127. well done http://www.google.com

    qwe - 30th March 2006 10:15 - #

  128. nice. i've done a similar thing once, that catches the html-page, replaces the input and select values with the appropiate post-values and then sends it to the user (big forms!). anyway, try adding an xml-namespace for your validiation so it validates against xhtml again. until xforms is finaly out and working (on the client side) this would be a cool class to have for my php.

    mg - 1st April 2006 21:04 - #

  129. vbn vbmbmvbm bvm bvm vbm mv bmv bmv mvm vm vmvmv m

    vbmvb - 4th April 2006 12:29 - #

  130. this is outstanding work.

    i am just starting php and can only hope that one day i will be able to understand how the hell this works...

    until that day.........................!

    paul - 7th April 2006 21:04 - #

  131. Like you I've been looking into a non-tedius way of error handling form input for a few years now - I love the simplicity of your solution - the only thing it lacks for me is some kind of js validation for supporting clients.. So i'm gonna toy with this and see if i can hack in some client side validation too - hopefully the result will kick-ass - thanks for sharing :)

    streaky - 13th April 2006 02:06 - #

  132. vsfsdfsd fsd

    324234 - 22nd April 2006 06:20 - #

  133. Anyone know how to change charset for xml parser? I want to change to this: &lt?xml version="1.0" encoding="iso-8859-2"?> Thx

    Fikus - 28th April 2006 23:59 - #

  134. hi!great job!i would like to ask you if I can use your code for my diploma paper on my university.i would be very gratefull to you if you allow me to.

    john - 1st May 2006 21:36 - #

  135. hi! great job!

    mangal - 10th May 2006 16:07 - #

  136. Whao! great job man i'm really impressed.

    samon - 12th May 2006 07:42 - #

  137. Hi! I want to know about what is EVS(Email Varification System). i have a form like your FormPrcessor form i want to do when i click on SUBMIT button it wiil store data in MySQL and generat an Email for user with this information like Your UserID:xxxxxxx Your Password : xxxxxxxxx And if You want to activate your account please click on this link

    vikas - 17th May 2006 07:31 - #

  138. uiiiiiiiiui

    iuiu - 18th May 2006 08:39 - #

  139. tre

    tre - 21st July 2006 11:55 - #

  140. ss

    ss - 21st August 2006 04:12 - #

  141. Thank you for your great article on php validation. Thanks for your great work!

    Max Developer - 3rd September 2006 21:37 - #

  142. s

    d - 19th September 2006 15:58 - #

  143. Love this class. But what if the form is not at the top of my page, but a bit down on a page with a lot of other content. When the user doesn't fill out the form correctly, it will be displayed again. Fine. But I don't get the script to jump directly to the form. Instead it shows the top of the page. I bet it's only a little entry in the class - but where?

    Nils - 20th September 2006 23:51 - #

  144. Nice class, but for some reason, the date field validation is not working for me... If I enter '09/21/2006', I get the message 'Your DOB must be in the form dd/mm/yyyy'! Any ideas on why this is not working???

    Jeff - 22nd September 2006 20:30 - #

  145. fhgfg

    fhgfhg - 3rd October 2006 20:16 - #

  146. sadvasvasvasv

    asdvasv - 3rd October 2006 20:19 - #

  147. a bit confude

    jeff - 6th October 2006 04:08 - #

  148. Don't understood.

    praveen sharma - 11th October 2006 08:56 - #

  149. asd

    adsad - 26th October 2006 17:21 - #

  150. 8

    8 - 26th October 2006 18:12 - #

  151. Jesus Christ is Lord and Savior. Give your life to Him and you will not regret it!

    Nelson - 26th October 2006 22:20 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/06/17/theHolyGrail

A django site