Nov 27, 2010
admin

The add page

We need a form to implement the add page. To get that done, we will make use of the Simple MVC forms. Let’s start as always, by the controller add action :

    /**
     * add Action
     *
     */
    public function addAction()
    {
        $this->model->bind($this->Request);
        $this->model->checkin();
        $this->view->form = new FeedForm('Feed');
        $this->view->form->initFromModel($this->model);
        // Handle eventual errors
        if (isset($this->Request['errors']) && is_array($this->Request['errors']))
        {
            $this->view->form->setErrors($this->Request['errors']);
        }
    }

The add action will use a FeedForm that we will create later. we can directly init the form the model using the initFromModel method. This will create text fields directly from the model properties, and take care of not displaying the ids.
Since the add method will also handle the errors if the informations are not filled for example, we need to add a test on the errors request variable and set those one to the view.
We will create a new form under business/front/forms/FeedForm.php :

class FeedForm extends Form
{
}

That’s all what we need to set for the form. It will work because the initFromModel will create dynamically the form elements. However, we can create independent forms with fields that are not binded to the model form. We will cover that later in a separate tutorial concerning Forms.
Lets code the view part now, under the folder : business/front/views/Feed/add.php add the following code.

<?php
echo $this->languages['add_feed']?&gt;&lt;/h3&gt;&lt;?php
echo $this->form->render();
?>

That’s all we need to do in the view, the render method of the view will handle all the magic. again, we can customize the form rendering using decoractors. This will be also covered in the Form section later.

The add page lacks only one thing. The part that will verify and save the feed. To implement this, we need to code the saveAction :

 
    public function saveAction()
    {
        $this->model->bind($this->Request);
        $form = new FeedForm('Feed');
        $form->initFromModel($this->model);
        if ($form->validate())
        {
            if ($this->model->save())
            {
                $this->Router->redirect('index', 'Feed', $this->language['redirect']['element_saved'], CoreView::MESSAGE_TYPE_SUCCESS);
            }
            else
            {
                $this->Router->redirect('index', 'Feed', $this->language['redirect']['element_not_saved'], CoreView::MESSAGE_TYPE_ERROR);
            }
        }
        else
        {
            $params = array_merge(array('id' => $this->model->id, 'errors' => $form->getErrors()));
            $this->Router->redirect('add', 'Feed', $this->language['redirect']['element_not_saved'], CoreView::MESSAGE_TYPE_ERROR, $params);
        }
    }

The save action will first bind the contents of the model from the Request. this method basically fills the instance properties from the Request. After, we instanciate a new FeedForm and we initialise it from the model. To validate the form, we simply call the $form->validate() method. The framework will verify that all the validators are satisfied. By default, Simple MVC adds a default validators to all properties which is a NotEmpty validator. This is what makes you have required fields.
If the form is validated and the model is saved, we redirect to the index action with a success message, otherwise, we redirect to the add page with the errors we got from the $form->getErrors() method.
That’s all! You can try your form now.

These few steps show how to do stuff with Simple MVC. Of course the stuff we explored here is basic. For more details, you can see the API Documentation or the other specialised tutorials. We are continually updating those section to expose what can be done with the framework.

2 Comments

  • C’est super cool votre framework .Je voterai ***** .
    par contre j’ai pas bien simuler comment va – t – on savoir dériger les urls dans le cas de plusieurs applis .Autre chose , une fois en ligne , il faut plus afficher les erreurs d’une facon aussi explicite!!!

    • Il y a plein de trucs qui manquent au framework. J’ai plus le temps de travailler la dessus comme avant.. si ca vous interesse, le git est la pour ca :-)

Leave a comment to Aroua wadie