Blog/SpeedHacking

No it's not a new illegal sport for crackers of the world. A little back story:

During my time as unemployed i followed an "get a job you lazy bum!" course, and we (me and the rest of the "students") decided to create a network - oh yes, the network word always comes up (actually i'm on LinkedIn now). Being the nerd of the group i was given the task of setting up a site for us. I initially thought, hey! let's just use one of the 4-gazzilion CMS systems available as Open Source. I tried one, Typo3, but it was very cumbersome to work with - and BIG as hell.

So what happened? Well i decided to try SpeedHacking (wow - back on subject!), or in other words: i implemented and designed the site as quickly as possible - as a test to see how fantastic my er2code project had gotten. I finished the page in 5 hours and 1 minute - complete with implementing and mucking about with the dreaded HTML and CSS (btw, the page validates as XHTML 1.0 Strict) not bad i think, but room for improvements.

One of the improvements is HTML_QuickForm a great tool for making form validations quick and easy - you don't even have to touch HTML. There is just a few rough edges that needs to be cleared up, first and foremost, setDefaults really need to bloody work for all elements, i've had troubles with multiple-select boxes before, and this time i was bit by a checkbox thingy. Basically you add items to your forms using addElement(type, name, title, attributes) all is well and fine there. That is, until you use setDefaults to set the values of the form, you know, when users want to update one or more fields in the database, the data already in the detabase should be the default values. For text fields this works fine, but for checkboxes it works shit! You would expect to be able to write something like:

1
2
3
$form = new HTML_QuickForm ( "editprofile", "get", $SCRIPT_URL );
$form->addElement ( 'checkbox', 'profilepublic', 'Should your profile be publicly availble?' );
$form->setDefaults ( array('profilepublic' => true) );

And the checkbox would be set, but ohhh no, it ain't that easy son! The checkbox then just gets a value attribute set to "1" - what's the deal with that? You would expect it to add checked="yes" or checked="checked" or something like it - but that ain't the case. This is really cumbersome since er2code now has an asArray method for all relations that just returns the given database row as a fieldName => fieldValue array. I should then be able to just write:

1
$form->setDefaults ( $profile->asArray() );

And everything should be dandy - but i guess not - same thing with the multiple selection box, you would think that you could just add a map from the name of the field to a list (array) of the elements you wish to add - but that's no good either, you have to manually extract the element from the $form and call setValues or something.

Oh, and while we are on it, how come PHP doesn't use references per-default, these two lines are apparently not the same, the first creates a copy of the element, the next receives the reference (pointer) to the already created element:

1
2
$i_am_copied = $form->getElement('element');
$i_am_a_reference &= $form->getElement('element');

PHP is just never going to be a nice language i guess.

Well, enough ranting, the code for the page is available from the stifinderedk category of my Repository if anyone want's to see er2code used in real life.

Speaking of real life, which is apparently what all the fuss is about, i've now been at GateHouse for ~10 days - and am starting to get my head around the project i'm working on. Interesting stuff, lot's of stuff to read, i actually get to see how Software Engineering is used in real life - not just some theoretical mambo-jambo - as i've had my troubles with in the past, ahem...

More on that another time.

Comments (0)

Post comment

If you wish, you can use markdown syntax in the comment field.