Programming with Nifty 1.2

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…

  1. Defines a form, containg an input field for “name”, a dropdown list for “nationality”, and a submit button.
  2. Loads a “User” object from the database, and binds the form to this object.
  3. Renders the form and sends it to the client.

When the postback occurs,

  1. The form processes it, feeding all the data into the bound object.
  2. Saves the object to the MySQL database.
  3. 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.)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>