Saturday, June 17, 2006

What is ActionForm? (5)

XCube_ActionForm is a device at web's requests for developer. But it doesn't connect to database. XOOPS Cube recommends separating ActionForm from business logics and database.
To use values which ActionForm received, you can make XoopsSimpleObject relay. XCube_ActionForm has interfaces to do it.

load(&$object)

You should implement the logic which copies values of $object to ActionForm in load(). XCube_ActionForm is difficult to have initial values. So you can use XoopsSimpleObject to set ActionForm. This method is important for edit function. If you send loaded object to ActionForm, it's re-edit contents. If you send new object to ActionForm, it's create contents. In other words, this is "Remove the difference of creating and editing".
In addition, you can use this member function for special loading. For example, ActionForm can copy a value as string from $object's integer value.

update(&$object)

You should implement the logic which copies values of ActionForm to $object. This member function has to be called after validation successful. As well as load(), you can use this member function for special updating. For example, "modified date time" field should not be copied from user's input values. In this case, you can always set time() to that field.
See example ActionForm in your myannounce module:

function update(&$obj)
{
$obj->set('update_unixtime', time());
$obj->set('subject', $form->get('subject'));
$obj->set('message', $form->get('message'));
}

This upload() doesn't overwrite 'create_unixtime'. And, it use time() for 'update_unixtime'. These techniques are used for protecting important field from users. For example, user's ActionForm should not handle the field which only administrators can edit. If you understand this concept, you can switch ActionForm by user type:

$actionForm = $xoopsUser->isAdmin() ?
new
EditForm4Admin() : new EditForm4User();

No comments: