Some features in the upcoming Nifty Framework 1.2 release:
<?php
class Index extends NF_Page
{
public function executeView($id)
{
global $Request, $Persistence, $Response;
$form = NF_Component::fromArray(array(
'component' => 'NF_CForm', 'name' => 'form',
array('component' => 'NF_CInputHidden', 'name' => 'id', 'dataField' => 'id'),
array('component' => 'NF_CInputText', 'name' => 'name', 'dataField' => 'name',
'label' => 'Name: '),
array('component' => 'NF_CSelect', 'name' => 'type', 'dataField' => 'type',
'label' => 'Nationality: ', 'options' => Nationality::getList()),
array('component' => 'NF_CButton', 'name' => 'submit',
'type' => NF_CButton::Submit, 'label' => 'Save!')
));
$form->dataObject = $Persistence->load('User', $id);
if (!$Request->isPost())
$Response->content = $form;
else
{
$form->processPostback();
$Persistence->save($form->dataObject);
$Response->redirect("/index/view/$id");
}
}
}
The whole thing…
- Defines a form, containg an input field for “name”, a dropdown list for “nationality”, and a submit button.
- Loads a “User” object from the database, and binds the form to this object.
- Renders the form and sends it to the client.
When the postback occurs,
- The form processes it, feeding all the data into the bound object.
- Saves the object to the MySQL database.
- Finally redirects back to the input form again.
I can’t make it shorter than that.
(There is also a configuration file, containing database parameters, a skeleton “User” class that holds three data fields – and one “Types” class that loads nationality lookups. Nothing more. This is a complete application.)