Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » More than one search field?

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

Page: 1
Go to End
Author Topic: 1360 Views
  • Mohammed
    Avatar
    Community Member
    25 Posts

    More than one search field? Link to this post

    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?

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: More than one search field? Link to this post

    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

  • Mohammed
    Avatar
    Community Member
    25 Posts

    Re: More than one search field? Link to this post

    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!!

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: More than one search field? Link to this post

    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);

    1360 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.