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

Alter field values before Filter


Go to End


1807 Views

Avatar
kimmings

Community Member, 2 Posts

7 November 2009 at 5:37am

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