Jump to content

Form Helper - Handling GET style filter forms


BikeHub

Recommended Posts

Posted

Hi,

I'm in the process of finally migrating our custom 3.x apps to 4.x and have a form helper related question. We've got a few scenarios where we currently use a GET form for list filtering in order to retain params in the URL.

From what I can see the Form Helper hard sets the method to POST for all forms. Digging through the code I noted that the in the Advanced Search feature (IPS\core\modules\front\search) a pseudo GET approach is taken by appending the relevant params and redirecting back to the form.

I've followed the Advanced Search example, but am stuck on an issue....

The page receives the initial POST, appends the params and redirects as expected, but then on subsequent POSTs any updated fields are ignored and instead the URL params are retained. Am I missing something... I think I've been staring at this too long. Please help me :blink:

Here is the code I'm testing with:

<?php


namespace IPS\hubregistry\modules\front\core;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * core
 */
class _core extends \IPS\Dispatcher\Controller
{
	/**
	 * Execute
	 *
	 * @return	void
	 */
	public function execute()
	{
		
		parent::execute();
	}

	/**
	 * ...
	 *
	 * @return	void
	 */
	protected function manage()
	{

		$_form = $this->_filterForm();

        // Store param values for search
		$search = array();
		$search['make'] = \IPS\Request::i()->bike_make;

		$pages = array(
			'count' => 0,
			'currentPage' => \IPS\Request::i()->page ?: 1,
			'perPage' => 20,
			'url' => \IPS\Request::i()->url(),
			'totalPages' => 0
		);

		$_limit = array(($pages['currentPage'] - 1) * $pages['perPage'], $pages['perPage']);

		$pages['count'] = \IPS\hubregistry\Incident::getIncidentsWithSearch( array(), \IPS\hubregistry\Incident::$databaseTable . '.date_created DESC', $_limit, $search, TRUE );

		$pages['totalPages'] = round($pages['count'] / $pages['perPage'], 0); 

		// Get incidents
		$incidents = \IPS\hubregistry\Incident::getIncidentsWithSearch( array(), \IPS\hubregistry\Incident::$databaseTable . '.date_created DESC', $_limit, $search);

        /* Online User Location */
		\IPS\Session::i()->setLocation( \IPS\Http\Url::internal( 'app=hubregistry&module=core&controller=core', 'front', 'stolenbikes' ), array(), 'loc_hubregistry_browsing' );

		// set breadcrumb
		\IPS\Output::i()->breadcrumb['module'] = array( \IPS\Http\Url::internal( "app=hubregistry&module=core&controller=core", 'front', 'stolenbikes' ), \IPS\Member::loggedIn()->language()->addToStack('__app_hubregistry') );

		\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack( '__app_hubregistry' );
		
        // Return the template view
		\IPS\Output::i()->output .= \IPS\Theme::i()->getTemplate( 'browse', 'hubregistry', 'front' )->index( $search, $incidents, $pages, $_form  );
 
	}

	/**
	 * Get the filter form
	 *
	 * @return	\IPS\Helpers\Form
	 */
	public function _filterForm()
	{

		// Create the form instance
		$form = new \IPS\Helpers\Form;

		// Init form fields
		$form->add( new \IPS\Helpers\Form\Text('bike_make') );

        /* If they submitted the advanced search form, redirect back (searching is a GET not a POST) */
		if ( $values = $form->values() )
		{

			$url = \IPS\Http\Url::internal( 'app=hubregistry&module=core&controller=core', 'front', 'stolenbikes' );

			if( isset( $values['bike_make'] ) AND $values['bike_make'] )
			{
				$url = $url->setQueryString( 'bike_make', $values['bike_make'] );
			}

			\IPS\Output::i()->redirect( $url );
		}

		return $form;

	}
	
}

 

Posted

Hello,

$form = new \IPS\Helpers\Form;

change to 

$form = new \IPS\Helpers\Form('form', 'save', \IPS\Http\Url::internal('app=hubregistry&module=core&controller=core', 'front', 'stolenbikes'));

 

Posted
9 minutes ago, newbie LAC said:

Hello,


$form = new \IPS\Helpers\Form;

change to 


$form = new \IPS\Helpers\Form('form', 'save', \IPS\Http\Url::internal('app=hubregistry&module=core&controller=core', 'front', 'stolenbikes'));

 

 

Thank you! I've been tearing my hair out over the weekend.

Seems so simple and obvious now that you point that out. Thanks again ?

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...