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.

All other Modules /

Discuss all other Modules here.

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

Displaying form on other Page from Generic View CollectionPage


Go to End


6 Posts   2156 Views

Avatar
Vladas

Community Member, 17 Posts

3 June 2010 at 1:18am

Hello,

I have my Generic View's module CollectionPage, where a $SearchForm and all results from my DataObject(called Job) are displayed.

Everything is fine, but what I need is to add the same $SearchForm to my HomePage, that would lead it's action to my CollectionPage once results are entered and submit button is pressed. The form is automatically generated by Generic View modules, so I can't replicate it, because it is generated based on $searchable_fields and also adds data to DropdownFields.

Any ideas on how could I grab that form and use it on my HomePage?

Thanks in advance.

Avatar
patjnr

Community Member, 102 Posts

6 June 2010 at 8:46am

HI

First you define a getCustomSearchContext method inside your DataObject Class like this

	function getCustomSearchContext() {
		$fields = $this->scaffoldSearchFields(array (
			'restrictFields' => array (
				'Description' => 'Description',
				'Approval_Status'=>'Approval_Status'
			)
		));
		$filters = array (
			'Description' => new PartialMatchFilter('Description'),
			'Approval_Status' => new ExactMatchFilter('Approval_Status')
		);
		return new SearchContext($this->class, $fields, $filters);

Create you form in mysite code

<?php
class YourSearchForm extends Form {

	function __construct($controller, $name) {
		
		$context = singleton('YourDataObject')->getCustomSearchContext();
		$fields = $context->getSearchFields();
		$actions = new FieldSet(new FormAction('doSearch', 'Search'));
		//$form = new Form($this, "SearchForm",$fields, $actions);
		
		parent :: __construct($controller, $name, $fields, $actions);
	}

	
	public function doSearch($data, $form) {
		$context = singleton('Classified')->getCustomSearchContext();
		$results = $context->getResults($data);
		return $this->customise(array(
			'Results' => $results
		))->renderWith(array('Classified_results', 'Page'));
	}
	
	
	function getResults($searchCriteria = array()) {
		$start = ($this->request->getVar('start')) ? (int)$this->request->getVar('start') : 0;
		$limit = 20;
	 
		$context = singleton('Classified')->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;
	}
}
?>

then Initiate your Form either at Page_Controller or HomePage_Controler like this

	function YourSearchForm () {
		return new YourSearchForm ($this, 'YourSearchForm ');
	}

HomePage.ss

   $YourSearchForm 

Avatar
patjnr

Community Member, 102 Posts

6 June 2010 at 8:54am

get the documentation form here

http://doc.silverstripe.org/searchcontext

thx

PJ

Avatar
Vladas

Community Member, 17 Posts

7 June 2010 at 11:30pm

This would really work out for me, and the form gets displayed if not one thing - returned results page.

On my domain/job-seekers is assign CollectionController page with a search form and lisf of results and when searching the results get updated straight away. That's what I would like to get: once the search query is entered on the homepage form, it is redirected to "job-seekers" page with a search executed to that data. Basically, I just need somehow to grab that form that would lead to job-seekers results.

Avatar
Vladas

Community Member, 17 Posts

9 June 2010 at 2:01am

bump

Avatar
Vladas

Community Member, 17 Posts

9 June 2010 at 10:52pm

I've tried this way:

<% control Page(job-search) %>
					
						$SearchForm
						
					<% end_control %>

Weird thing is that if I change $SearchForm into $Title, it displays title of /job-search/ page properly, but it doesn't show the $SearchForm on my HomePage. Any ideas?