Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Show search parameters in Results Page (CustomSearchContext)

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

Page: 1 2
Go to End
Author Topic: 2077 Views
  • VRoxane
    Avatar
    Community Member
    42 Posts

    Show search parameters in Results Page (CustomSearchContext) Link to this post

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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

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

  • VRoxane
    Avatar
    Community Member
    42 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

    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 !

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

    Its the same FieldsSet as you posted.

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

  • VRoxane
    Avatar
    Community Member
    42 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

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

  • VRoxane
    Avatar
    Community Member
    42 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

    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.

  • VRoxane
    Avatar
    Community Member
    42 Posts

    Re: Show search parameters in Results Page (CustomSearchContext) Link to this post

    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 ?

    2077 Views
Page: 1 2
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.