Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Alter field values before Filter

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

Page: 1
Go to End
Author Topic: 1076 Views
  • kimmings
    Community Member
    2 Posts

    Alter field values before Filter Link to this post

    I'm not sure if i'm even doing this correctly so bear with me...
    I'm trying to pull results based upon 2 numeric values (min and max size).
    I have the GreaterThanFilter and LessThanFilter filters working for a SearchContext (or at least in some manner).
    Unfortunately if a size is say 300 and the user enters 300 for the min size the record won't show because the size in the db is not greater than the size defined by the user.
    I tried combining the ExactMatchFilter but then no results were shown.
    What i need is an GreaterThanOrEqualToFilter!
    So ... in my doSearch method (the action on the search form) I tried to alter the values passed through the $data Array and when I write these out to screen they appear to have been changed ($data[minSize] -= 1) but the results still don't show.

    am I over complicating this?

    // get Custom Search Context Method in Class
       function getCustomSearchContext(){
          $fields = singleton('PropsPage')->scaffoldSearchFields(array(
                'restrictedFields'=>array( 'Size' )
          $filters = array(
             'minSize' => new GreaterThanFilter('Size'),
             'maxSize' => new LessThanFilter('Size')

          return new SearchContext( singleton('PropsPage')->class, $fields, $filters );

    and the form action is 'doSearch' in Controller

       public function doSearch( $data, $form ) {
          // amend the min max size to allow for the actual input value
          if($data[minSize] != 0){    $data[minSize] -= 1; }
          if($data[maxSize] != 0){    $data[maxSize] += 1; }
    // print_r($data) shows that the values have been changed
          $results = $this->getResults($data);
          return $this->customise(array(
             'Results' => $results
          ))->renderWith(array('Prop_results', 'Page'));

    function getResults($searchCriteria = array()) {
          // $searchCriteria shows minSize to be 1 less than value inputted... great!
          $start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
          $limit = 10;
          $context = singleton('PropsHolder')->getCustomSearchContext();
          $query = $context->getQuery(       $searchCriteria, null, array('start'=>$start,'limit'=>$limit) );
          $records = $context->getResults(    $searchCriteria, null, array('start'=>$start,'limit'=>$limit) );
          if($records) {
             $records->setPageLimits($start, $limit, $query->unlimitedRowCount());
          return $records;

    Not sure if there's an easier way to achieve this...

    any help would be great

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.