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

Pagination Links not passing on search form data


Go to End


2086 Views

Avatar
FokeyJoe

Community Member, 1 Post

22 August 2009 at 11:45am

Hiya... I'm doing a rebrand of an existing SS site, I've run into a problem with the search form. It had been set up straight out of the tutorial. I've needed to do a hand-written the form in the template (basing it on what SS was originally doing as much as possible). It produces the initial results page okay, but it's not passing the search vars as GETs on to the next/prev and page links and I can't see why. I'm not well-versed enough in the ways of the sapphire codebase to see exactly how or where they get added.

I had to add $form = $this->SearchForm() in my results() because the $form arg was being passed null, which might explain my troubles - but I can't work out why it is null.

Here's the relevant bit of my PageController (the extra gumf is for doing 11-20 of 88 results):

    function SearchForm() {
        $searchText = isset($this->Query) ? $this->Query : '';
        $fields = new FieldSet(
            new TextField("Search", "", $searchText)
        );
        $actions = new FieldSet(
            new FormAction('results', 'Search', 'Search', 'btn_search')
        );
        return new SearchForm($this, "Search", $fields, $actions);
    }

    function results($data, $form){
        $showPerPage = 10;
        $form = $this->SearchForm();
        $results = $form->getResults($showPerPage);
        $firstItemIndex = ($showPerPage * ($results->CurrentPage() - 1)) + 1;

        $data = array(
            'Results' => $results,
            'Query' => $form->getSearchQuery(),
            'Title' => 'Search Results',
            'FirstItemIndex' => $firstItemIndex,
            'LastItemIndex' => $firstItemIndex + $showPerPage - 1,
            'ShowPerPage' => $showPerPage,
        );
        return $this->customise($data)->renderWith(array('Page_results', 'Page'));
    }

And the search form looks like:

<form method="post" action="/home/results" name="Search">
  <fieldset class="search">
    <label for="searchfield">Search the site:</label>
    <input type="text" class="searchfield" id="searchfield" value="" name="Search" size="16" maxlength="100">
    <input type="submit" class="btn_search" title="Search" name="SearchBtn" value="Search" >
  </fieldset>
</form>