Your answer is clear

Colin's Blog

Nested
Automatically building forms with WebGUI::Crud
User: perlDreamer
Date: 4/30/2009 11:46 am
Views: 1256
Rating: -1    Rate [
|
]

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.

  1. Add label and hoverhelp fields to each property in the crud_definition properties.  WebGUI::Crud will happily ignore these extra fields.
  2. Build an instance of the object you want to edit. It can be a blank one, or an existing one that you want to change.  Then, make a WebGUI::HTMLForm object, and flesh it out like this:

##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.

Reply
Re: Automatically building forms with WebGUI::Crud
User: patspam
Date: 5/5/2009 2:22 am
Views: 165
Rating: 0    Rate [
|
]

Nice one!

 

Reply
PreviousNextAdd
blue divider bar

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