stateful forms

can anybody explain, how to use stateful forms and what they are used for?

there is an example in the hangman demo but I dont get it

These type of forms are similar to how Prado keeps the state of a page throughout subsequent POST requests. The basic idea is, that there’s a hidden field, that can contain arbitrary information. You can use setPageState() to save a name/value pair in this page state. With every POST, this state information is submitted with the request and available in the next action through getPageState(). If the page is requested via GET, all state will be lost.

The hangman demo uses this to store information of the current game. One advantage of this method is, that it’s independent of sessions and thus a user can even have multiple browser windows open with different games.

Actually is a way to store some application data in the result html code and get them back at the next request.

E.g. a page has counter that shows how many times a button on the same page is clicked.

With statefulForm, the action that handles this, could be:

  public function actionCounter()


    $count = $this->getPageState('clicks', 0);

    $this->setPageState('clicks', $count + 1);

    $this->render('counter', array('count'=> $count,));


Where ‘clicks’ is an arbitrary name that is used as array key, and 0 is the default value if no ‘clicks’ PageState have set before.

Counter view could now be:

<h2>You have clicked <?php echo $count?> times</h2>

<?php echo CHtml::statefulForm(); ?>

<?php echo CHtml::submitButton('Click Me!'); ?>


This would also be possible with sessions (see cookies)

I hope this helps