Willowgarden Highlight: Easy Processing of Form Input
Welcome to this week's installment of Willowgarden Highlights. I'll be talking about processing form input and saving it via an archetype (domain model object), a very common task of any Web application.
In the bad old days of processing forms and saving each field to a column in a database row, you'd have to do a lot of manual gruntwork: accessing each field from the $_POST superglobal, checking the values, constructing the SQL query, connecting to the database and executing the query, and so forth. Wouldn't it be great if you could just import the form right into an object, and let it do the validation and the database saving based on just a few simple rules? Let's take a quick look at how that might work.
The easiest way to create a form ready for import is to use the WFFormCreator class that comes with Willowgarden. Here's an example of a form for entering a project task:
-
<?php
-
$form = new WFFormCreator();
-
$form->useNamePrefix("ProjectTask"); // this is important
-
?>
-
<table>
-
<tr>
-
</tr>
-
<tr>
-
<td>Comments:</td> <td><?php echo $form->newTextarea("name: comments", "rows: 10", "cols: 60") ?></td>
-
</tr>
-
</table>
-
<?php
-
?>
So what we're doing here is creating a form that will call the "create" action of the current page when the form is submitted. In the form, we output a textfield for the task name and a textarea for comments. But notice that line of code near the top that calls the useNamePrefix method of the form object? That sets up the form to use that prefix in the field name for each form element. So the actual name of the comments field isn't "comments" but is in fact "ProjectTask.comments". (Periods get translated to underscores in PHP, so in PHP you'd get "ProjectTask_comments" in your $_POST superglobal.) And of course, the name of the "name" field is in fact "ProjectTask.name".
Now in your page controller, you can write some very simple code to process the incoming form. Here's an example:
-
public function create_action()
-
{
-
$task = new ProjectTask();
-
$task->importFromArray($_POST);
-
if (!$task->isValid()) {
-
$this->getRenderer()->assign("validationErrors", $task->getValidationErrors())
-
->assign("task", $task)
-
->render("add");
-
}
-
else {
-
$task->save();
-
WFRegistry::getRequest()->redirect("view/{$task->id}", "action");
-
}
-
}
You can start to see where we're going with this. In the "create" action, we're instantiating a new ProjectTask object (a subclass of WFArchetype). Then we're importing data from the $_POST superglobal (which is by default filtered by the input filter that comes with Willowgarden to prevent Javascript and CSS hacks). The archetype is smart enough to check for the prefix of each form field to see if matches its own class name and import only those values. Then we're checking to see if the archetype is valid (i.e., all the form fields now stored in the archetype validate according to the archetype's validation rules) -- if it isn't, we'll obtain the validation errors and render the add form again; if it is, we'll save the archetype and redirect the browser to the view action with the new task's "id" as a path parameter.
Showing how to set up the ProjectTask archetype with database access and validation rules is beyond the scope of this article, but I hope now have a sense for how Willowgarden's Logic and Render layers (Controller and View layers in MVC parlance) work together very well to make your job as a Web developer much easier. One of the great things about the name prefix feature is that you can use multiple prefixes in the same form, allowing you to import the exact same form array into multiple archetype classes without causing any conflicts. You can also use prefixes for the id of form elements, which is a feature I'm using a lot in one of my applications.
And that's it for today's Willowgarden Highlight! Come back soon to learn more about this exciting new application platform for PHP 5.

Del.icio.us
Cosmos
Digg
September 19th, 2006 at 12:21 pm
Jared White’s Blog: Willowgarden Highlight: Easy Processing of Form Input…
…