UML Class Diagrams
Throughout the years I’ve written many game engines. The thing about game engines is that they’re big and therefore a good design will make your life easier later in development. Because of this I’ve taken an interest in software design. For my most recent game engine design, I’ve employed the use of UML diagrams, which I usually jot down with paper and pencil. Unfortunately paper isn’t very good for a diagram that gets modified often, so I went searching for online tools to help. I found one, but didn’t like the interface very much, so I decided to implement my own.
It’s called SUML and is written in javascript using the Scriptaculous library. Because Javascript doesn’t have access to the filesystem the save feature is a little weird - you’ll have to copy the data from the textarea into a file to keep it around. Using a PHP script to output a file is an option that I might consider for a later version, however. Defining methods is a feature I want to add later as well. Also, I’m pretty sure it doesn’t work in IE. Without further adieux: SUML
No commentsOn Workflow Apps, Part 1 - Form Validation in PHP
When I work on web applications at my job, one major area to consider is form validation. Some fields may be mandatory, some must only have digits, etc,.
Most of the applications I work on are work flow apps - person X submits form A, members of group Y revise or approve form A, person Z manages group Y and gives final approval for form A, and so on. Because of the many levels of bureaucracy involved, the work flow changes constantly - during development and after.
When my boss told me that he had a new work flow application for me, I wanted to try a new approach by utilizing some object oriented design techniques. The forms of the application are one of things that are changed most often by the many committees overseeing these projects. Taking this into consideration, I developed a form class that would handle form validation with an easy-to-use interface. Here’s an example of it in action (links to the code are at the bottom of the page), and here’s how one would use it:
// create validators
$phonenumChecker = new RegexValueChecker( "/\d/",
array( 7, 10, 11 ), "Phone numbers must have 7, 10, or 11 digits" );
$alphanumChecker = new RegexValueChecker( "/[^a-zA-Z0-9_\s/",
0, "Only alphanumeric characters allowed" );
// create fields
$namefield = new FormField( "Name", $alphanumChecker );
$phonefield = new FormField( "Phone", $phonenumChecker );
$bandfield = new FormField( "Band", $alphanumChecker );
$albumfield = new FormField( "Album", $alphanumChecker );
// form field map collects many fields into one
// (cdfield holds a band name and an album name)
$cdfield = new FormFieldMap( "CD", array( $bandfield, $albumfield ) );
// form field array holds zero to many of one form field
// (cdsfield holds zero to many 'cdfield's)
$cdsfield = new FormFieldArray( "CDs", $cdfield );
// create form
$myform = new Form;
$myform->AddField( $namefield );
$myform->AddField( $phonefield );
$myform->AddField( $cdsfield );
$myform->SetValues( $_GET );
// get errors, if there are any
$ErrorStrings = array( );
$Values = $myform->GetValues( );
$Errors = $myform->GetErrors( $ErrorStrings );
// set up a Smarty template
$Template = new Smarty;
if( $_REQUEST[ "submit" ] ) {
$Template->assign( $Values );
$Template->assign( “Errors”, $Errors );
$Template->assign( “ErrorStrings”, $ErrorStrings );
}
$Template->display( “index.tpl” );
And it’s just that simple! If you’re not familiar with it, Smarty is a templating engine that will allow you to replace parts of an html file with data. While the form class does not depend on Smarty, it makes using the form class a breeze. The form class also leverages PHP’s ability to store form fields as arrays. For example, if i have two form fields with the same name, (<input name=”a” /> … <input name=”a” />) i can append a pair of square brackets (<input name=”a[]” />) and PHP will see the form field as an array.
The use of FormFieldMap and FormFieldArray might be a little confusing, but their purpose was to support nested sections of forms. I wrote them because the particular app I’m working on has a complex form that has to support a variable number of certain fields. I couldn’t think of any better names for them - I named FormFieldMap after the STL map in C++.
The PHP code is available here, the Smarty template HTML here, and some javascript that it uses here. Hopefully this will improve coding time and maybe cut down input related security vulnerabilities.
2 commentsWelcome
Out of boredom I finally decided to install Wordpress. And this is where I will be blogging about computer science and other related things. More to come, soon.
Also, I need a catchy name for my blog.
2 comments