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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

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

CustomSearchForm snippet

Go to End

46 Posts   14242 Views


Community Member, 35 Posts

28 June 2012 at 2:31am

I am trying to get this working with 2.4. I have a clean install where I am testing this locally but getting a blank results page ( the url is http://localhost/home/SearchForm?Search=Search&action_results=Search )

I cant see anything wrong with my code. I do have a results template. Any idea what might be causing this?


Community Member, 35 Posts

2 July 2012 at 9:59pm

I have got this working since. My problem was the results layout file I was using.

I now have a challenge to show search results based on a linked DO. So I want to list search results form the parent DO based on matches from the child DO. Anyone done anything similar or aware of this can be achieved?


Community Member, 10 Posts

5 December 2012 at 1:30am

I have optimized this script for Silverstripe 3. If anyone needs it just leave a message and i will upload the script.


Community Member, 6 Posts

5 December 2012 at 3:05am

Hello Leon223,

I am interested in your SS3.0 version of this script.


Community Member, 9 Posts

4 February 2013 at 10:58pm

Hello there,

I too am interested in the optimized script. Any chance it can still be uploaded?

Thanks in advance.

Tony Air

Community Member, 13 Posts

5 February 2013 at 6:32am

Edited: 05/02/2013 6:42am

Here's the other SS3 snippet


public function SearchForm(){
		$searchText = _t('SearchForm.SEARCH', 'Search this site ...');

		if($this->request && $this->request->getVar('Search')) {
			$searchText = $this->request->getVar('Search');
		$fields = new FieldList(
			$textField = TextField::create('Search',$searchText)
		$validator = new RequiredFields('Search');
		$actions = new FieldList(FormAction::create('doSearch','Go'));
		$form = new ZZSearchForm($this,'SearchForm',$fields,$actions,$validator);
		return $form;
public $isSearched = false;
	public static $classesToSearch = array('SiteTree');
	public function doSearch($data, $form, $request) {
		$this->isSearched = true;
		$limit = 10;
		$tplData = array(
			'PageResults' => $form->getResultsByClass('SiteTree'),
			'Query' => $form->getSearchQuery(),
			'Title' => _t('SearchForm.SearchResults', 'Search Results')
		$ar = $this->extend('updateDoSearch',$tplData,$form);
		if(!empty($ar)){$tplData = $ar[0];} // dirty
		return $this->owner->customise($tplData)->renderWith(array('Page_results', 'Page'));
public function isSearched(){
		return $this->isSearched;

project / _config.php

Page_Controller::$classesToSearch = array('SiteTree','CatItem'); // set classes to search


public static $searchable_fields = array('Title','Model','Manufacturer'); // searchable fields


public function updateDoSearch($tplData,$form){
		$tplData['CatItemResults'] = $form->getResultsByClass('CatItem')->setPageLength(9); // here u can set any additional data lists for ur templates
		return $tplData;

Then u can parse ur variables in template any way u need, here's my template for an instance:

<div id="PageContainer" class="typography">
	<div id="HeaderContainer">
	<% if Query %>
		<div class="h2">
			<%t Page.RESULTSQUERY "You searched for &quot;{Query}&quot;" Query=$Query %>
	<% end_if %>
	<div class="h3">We have found following Pages</div>
	<% include PageSearch %>	
	<div class="h3">We have found following Items</div>
	<% if CatItemResults %>
	<div id="BasketItems" class="row">
		<ul class="basketItems clearfix">
			<% loop CatItemResults %>
				<% include CatItemTeaser %>
			<% end_loop %>
		<% with CatItemResults %>
			<% include Pagination %>
		<% end_with %>
	<% else %>
		<div class="h3"><%t Page.RESULTSNO "Sorry, your search query did not return any results." %></div>
	<% end_if %>

<% if MoreThanOnePage %>
<div class="pagination pagination-centered">
	<li class="<% if not NotFirstPage %>disabled <% end_if %>prev">
		<a href="$PrevLink">&laquo;</a>
	<% loop Pages %>
		<li<% if CurrentBool %> class="active"<% end_if %>>
			<% if Link %>
				<a href="$Link">$PageNum</a>
        	<% else %>
			<% end_if %>
	<% end_loop %>
	<li class="<% if not NotLastPage %>disabled <% end_if %>next">
		<a href="$NextLink">&raquo;</a>
<% end_if %>

Go to Top