Making Your Search Forms Keep State

February 27, 2012 1:36 PM

We roll out so many CRUD (Create Read Update Delete) pages in the course of our projects and life for that matter.  And so often there's a list or grid view for all the existing entries and of course, there's the obligatory search form at the top.  How this should behave is probably up for debate but in my opinion the best way is that while you're in that section of the site the search data should remain.  So you filter the list, edit one of the records and then when you go back to the list the search data is still there.  If you go elsewhere and come back, well, the search data resets.

So, what's a good pattern for dealing with this requirement, a real simple way is to simply store these values in your flash data.  If you're unfamiliar with flash data, this is extremely volatile data that is only available during the current and next request.  So, we just need to set this data, with the user submitted values, when they do a search and then persist it while we remain in this section of the site.  If you're using MVC (Model View Controller) then this becomes simpler, in your action, set the data, examples here are based on development with Yii! :

public function actionIndex()
    $model = $user->getFlash( self::searchFlash, null, new SearchForm() );
    $form = new CForm( 'application.views.upload._forms.searchForm', $model );
    if ( isset( $_GET['SearchForm'] ) )
        $model->attributes = $_GET['SearchForm'];
        Yii::app()->user->setFlash( self::searchFlash, $model );



Then we persist this by running some code during controller initialisation:

public function init()
    if ( Yii::app()->user->hasFlash( self::searchFlash ) )
        $user->setFlash( self::searchFlash, $user->getFlash( self::searchFlash, null, false ) );



Sewana at Oct 26, 2016 10:48:49 AM
I'll try to put this to good use immytiadele.
Lily at Oct 27, 2016 7:30:31 AM
This forum needed <a href="">shkinag</a> up and you've just done that. Great post!
Sagi at Oct 29, 2016 6:16:56 AM
Help, I've been informed and I can't become <a href="">ignroant.</a>