
WebGUI has it's own little ORM module called WebGUI::Crud. It provides all the usual WebGUI style of database interactions, as well as allowing easy table creating, deleting and altering tools. In addition it comes with a default way to post process forms. However it does not provide a way to automatically build those forms, which need to be post processed.
Well, it turns out with a few lines of glue code, you can build those forms automatically from the crud_definition. Last year, WebGUI::HTMLForm got a dynamicForm method, which, when passed an Asset or Workflow Activity style definition data structure, would automatically build the edit form for you.
Starting to get the idea?
A crud_definition contains a properties key, which contains most of the pieces of a standard definition. So, here's what you have to do to make it work.
##Example taken from WebGUI::PassiveAnalytics::Flow, www_editRule
my $form = WebGUI::HTMLForm->new($session);
$form->hidden( name=>"op", value=>"passiveAnalytics");
$form->hidden( name=>"func", value=>"editRuleSave");
$form->hidden( name=>"ruleId", value=>$ruleId);
$form->dynamicForm(
[WebGUI::PassiveAnalytics::Rule->crud_definition($session)],
'properties',
$rule
);
$form->submit;
You can see that I've had to manually setup form variables to tell the browser which content handler (op) and method (func) to call, and a submit button. $form->dynamicForm builds the rest of the form for me. I had to wrap the crud_definition in an array reference, since that's how a standard definition is built. Next, I told it which field to take for the list of properties, and I had to pass it an object, so that the form could be filled with default or existing values. editRuleSave does form value validation, then calls $rule->updateFromFormPost to update both the current object, and the database.
Most of that edit form code could be encapsulated in a WebGUI::Crud convenience method, but, even as it is, it's far simpler than building it by hand.
Nice one!

Copyright 2010 perlDreamer Consulting | All Rights Reserved | Site Map | Graphic Design by Plain Black