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.

Form Questions /

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

More than one search field?


Go to End


4 Posts   2073 Views

Avatar
Mohammed

Community Member, 25 Posts

23 December 2009 at 2:21pm

Edited: 23/12/2009 2:26pm

Hi guys,

I'm trying to add two search fields onto my site-one in the header and one in the footer. (To have valid HTML, I need them to have different ID tags; hence why I duplicated the code.) The search field in the header works fine, however, the one in the footer does not.

mysite/code/Page.php

class Page_Controller extends ContentController {
	...
	function SearchForm() {
		  $searchText = isset($this->Query) ? $this->Query : 'Search';
		  $fields = new FieldSet(
			 new TextField("Search", "Search", $searchText)
		  );
		  $actions = new FieldSet(
			 new FormAction('results', 'Search')
		  );
		  return new SearchForm($this, "SearchForm", $fields, $actions);
	}
	function Searchformfooter() {
		  $searchText = isset($this->Query) ? $this->Query : 'Search';
		  $fields = new FieldSet(
			 new TextField("SearchFooter", "Search", $searchText)
		  );
		  $actions = new FieldSet(
			 new FormAction('results', 'Search')
		  );
		  return new SearchForm($this, "Searchformfooter", $fields, $actions);
	}
	function results($data, $form){
      $data = array(
         'Results' => $form->getResults(),
         'Query' => $form->getSearchQuery(),
         'Title' => 'Search Results'
      );
      $this->Query = $form->getSearchQuery();
      return $this->customise($data)->renderWith(array('Page_results', 'Page'));
    }

...

}

Any suggestions on how to get them both working?

Avatar
MateuszU

Community Member, 89 Posts

23 December 2009 at 3:48pm

In this line:

new TextField("SearchFooter", "Search", $searchText)

You have set the field id to SearchFooter. I believe the SearchForm::getResults expects it to be named "Search". Lemme know if that's it :)

Avatar
Mohammed

Community Member, 25 Posts

23 December 2009 at 3:56pm

Edited: 23/12/2009 4:05pm

Well, if I change "SearchFooter" back to "Search", then I run into the initial problem of having two of the same ID tags on the page; a nono with HTML validation (I received a validation error like #IDs are more specific, .class should be used instead...)

Line 224, Column 13: ID "Search" already defined

			<div id="Search" class="field text "><label class="left" for="SearchForm_Sear

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).

I need at least one of the two IDs to be different. By changing it to "SearchFooter" I was able to get a different ID, at the expense of the search not working.

:/

There has to be a way to define another search field...I just don't know how. Thanks for trying!!

Avatar
MateuszU

Community Member, 89 Posts

5 January 2010 at 11:52am

Edited: 05/01/2010 11:53am

You're right, that first param gets passed into the ID.

So maybe create another class FooterSearchForm extending SearchForm, redefine getResults to get the $keywords from proper place:

$keywords = $data['SearchFooter'];

and then use that field in your footer search form. I'm not sure if that's the "right" solution though :)

return new FooterSearchForm($this, "Searchformfooter", $fields, $actions);