Jump to:

10851 Posts in 2517 Topics by 1793 members

All other Modules

SilverStripe Forums » All other Modules » Displaying form on other Page from Generic View CollectionPage

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1259 Views
  • Vladas
    Avatar
    Community Member
    17 Posts

    Displaying form on other Page from Generic View CollectionPage Link to this post

    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.

  • patjnr
    Avatar
    Community Member
    101 Posts

    Re: Displaying form on other Page from Generic View CollectionPage Link to this post

    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

  • patjnr
    Avatar
    Community Member
    101 Posts
  • Vladas
    Avatar
    Community Member
    17 Posts

    Re: Displaying form on other Page from Generic View CollectionPage Link to this post

    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.

  • Vladas
    Avatar
    Community Member
    17 Posts
  • Vladas
    Avatar
    Community Member
    17 Posts

    Re: Displaying form on other Page from Generic View CollectionPage Link to this post

    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?

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