Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Show search parameters in Results Page (CustomSearchContext)


Go to End


10 Posts   3583 Views

Avatar
VRoxane

Community Member, 42 Posts

8 September 2010 at 3:29am

Hi all !

Below is my search function, BienImmo being my DataObject...

public function getCustomSearchContext() {
		$locs = DataObject::get('Localite');
			if ($locs) {
				$locs = $locs->toDropdownMap('ID', 'Ville', ' - Ville - ', true);
			}

		$fields = new FieldSet(
			new CheckboxSetField(
				$name = "Loc_Ville",
				$title = "Ville(s) :",
				$source = $locs,
				$value = ""
			),
			new TextField('PrixMin', 'Prix Min'),
			new TextField('PrixMax', 'Prix Max')
		);
		$filters = array(
			'Loc_Ville' => new ExactMatchMultiFilter('LocaliteID'),
			'PrixMin' => new GreaterThanFilter('Prix'),
			'PrixMax' => new LessThanFilter('Prix')
		);
		
		return new SearchContext(
			$this->class, 
			$fields, 
			$filters
		);
	}

I need to remember the search parameters in the Results Page.
How can I get those in the results page template ?

A better thing would be to re-fill the form with those parameters (checkboxes checked, fields filled....).
Would it be possible ? Is SilverStripe THIS magical ? ;)

Avatar
Martijn

Community Member, 271 Posts

8 September 2010 at 4:06am

Short version:

$Loc_Ville = $this->request->postVar('Loc_Ville') ? $this->request->postVar('Loc_Ville') : '';
$PrixMin = $this->request->postVar('PrixMin') ? $this->request->postVar('PrixMin') : '';
$PrixMax= $this->request->postVar('PrixMax') ? $this->request->postVar('PrixMax') : '';

$fields = new FieldSet(
new CheckboxSetField(
$name = "Loc_Ville",
$title = "Ville(s) :",
$source = $locs,
$value = $Loc_Ville
),
new TextField('PrixMin', $PrixMin),
new TextField('PrixMax', $PrixMax)
);

Avatar
VRoxane

Community Member, 42 Posts

8 September 2010 at 4:48am

Thanks Martijn ! ... but where do I put those lines ?

I understand it names the variables so I can access them ... but I'm a bit lost, can you explain how to use this code ?

Seems your short version is too short for my SilverStripe knowledge ! :P

Avatar
Martijn

Community Member, 271 Posts

8 September 2010 at 4:52am

Its the same FieldsSet as you posted.

Replace the FieldsSet with one I posted and put the 3 lines above it.

Avatar
VRoxane

Community Member, 42 Posts

8 September 2010 at 4:55am

I get this :

Fatal error: Call to a member function postVar() on a non-object in E:\Program Files (x86)\wamp\www\modele_Immo\mysite\code\BienImmo.php on line 54

Avatar
Martijn

Community Member, 271 Posts

8 September 2010 at 5:12am

Try this:

$Loc_Ville = '';
$PrixMin = '';
$PrixMax = '';

if(isset($this->request)){
$Loc_Ville = $this->request->postVar('Loc_Ville');
$PrixMin = $this->request->postVar('PrixMin');
$PrixMax = $this->request->postVar('PrixMax');
}

Avatar
VRoxane

Community Member, 42 Posts

8 September 2010 at 5:18am

Thank you very much for replying to fast !

Okay that works I have no errors.
The titles of the textfield disapeared, though.

How do I access the parameters after the search ?
Putting $Loc_Ville in my template didn't do anything.

Avatar
VRoxane

Community Member, 42 Posts

8 September 2010 at 5:23am

I made the titles of the fields reappear with

new TextField('PrixMin', 'Prix minimum', $PrixMin),

Any idea of how to get the Villes or the Prix Min and Prix max in my results page ?

Go to Top