Jump to:

3431 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Scaffolded search form shows only one field

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

Page: 1
Go to End
Author Topic: 1293 Views
  • Euphemismus
    Avatar
    Community Member
    82 Posts

    Scaffolded search form shows only one field Link to this post

    Hi folks,

    at the moment I'm working on some portal and encountered a problem with scaffolded search forms. I'm using a code example from the wiki to create a search form in my application.
    Everything works fine, except the fact that I only get one field for the search. As far as I understood the magic, there should be one search field per defined (whitelist) field in my getCustomSearchField() method.
    I did some debug on the $fields with Debug::dump() which showed me a larger object structure where I could only find the "title" and some recursions, but not the other defined fields.
    I double-checked my database declarations in the model and all fields are there, icluding the correct data types.

    Any ideas?

    code:

       public function getCustomSearchContext()
       {
          $fields = $this->scaffoldSearchFields( array(
                      'restrictFields' => array(
                                           'Title',
                                           'Content',
                                           'Instrument',
                                           'Plz',
                                           'Location',
                                           'Ambition',
                                           'ExpiryDate'
                                        )
                   )
                );

          $filters = array(
                   'Title' => new PartialMatchFilter( 'Title' ),
                   'Content' => new PartialMatchFilter( 'Content' ),
                   'Instrument' => new ExactMatchFilter( 'Instrument' ),
                   'Plz' => new PartialMatchFilter( 'Plz' ),
                   'Location' => new ExactMatchFilter( 'Location' ),
                   'Ambition' => new ExactMatchFilter( 'Ambition' ),
                   'ExpiryDate' => new LessThanFilter( 'ExpiryDate' ),
                );

          return new SearchContext(
                   $this->class,
                   $fields,
                   $filters
                );
       }

  • AlaVive
    Avatar
    Community Member
    42 Posts

    Re: Scaffolded search form shows only one field Link to this post

    I just came across this problem yesterday. You have to declare the searchable fields first in your controller. (At least, that's what worked for me-- someone else may offer a cleaner explanation):

    static $searchable_fields = array(
    'Title' => 'PartialMatchFilter',
    'Content' => 'PartialMatchFilter',
    'Instrument' => 'ExactMatchFilter',
    'Plz' => 'PartialMatchFilter',
    'Location' => 'PartialMatchFilter',

    );

    (etc., etc...)

    Does this help?

  • Euphemismus
    Avatar
    Community Member
    82 Posts

    Re: Scaffolded search form shows only one field Link to this post

    Hi AlaVive,

    it worked! I tested it with the additional $searchable_fields array - that did the trick.
    So I will comment on the tutorial mentioned above, that the definition of searchable fields is needed (SS 2.3.3).
    Thank you

    1293 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.