I need help with a search form that won’t show results
I’m working on a custom search feature for events in SilverStripe 5. The search form displays correctly in my EventSearchPage.ss template, and the form submission works fine. The controller method processEventSearch() can filter the events based on user input, but the filtered results don’t appear below the form.
Here’s my controller code:
<?php
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FormAction;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\Control\Controller;
class EventSearchPageController extends PageController
{
private static $allowed_actions = [
'EventFilterForm',
'processEventSearch',
];
public function EventFilterForm(): Form
{
$categories = [
'Conference' => 'Conference',
'Workshop' => 'Workshop',
'Seminar' => 'Seminar',
'Meeting' => 'Meeting',
'All' => 'All'
];
$venues = [
'Indoor' => 'Indoor',
'Outdoor' => 'Outdoor',
'Virtual' => 'Virtual',
'Mixed' => 'Mixed',
'All' => 'All'
];
$fields = FieldList::create(
TextField::create('EventTitle')
->setAttribute('placeholder', 'Search event name')
->addExtraClass('form-control'),
TextField::create('EventDate', 'Event date')
->setAttribute('placeholder', 'DD-MM-YYYY')
->addExtraClass('form-control'),
DropdownField::create('Category', 'Event category')
->setSource($categories)
->addExtraClass('form-control'),
DropdownField::create('Venue', 'Venue type')
->setSource($venues)
->addExtraClass('form-control')
);
$actions = FieldList::create(FormAction::create('processEventSearch'));
$form = Form::create($this, 'EventFilterForm', $fields, $actions);
return $form;
}
public function processEventSearch(HTTPRequest $request)
{
$data = $request->postVars();
$eventTitle = $data['EventTitle'];
$eventDate = $data['EventDate'];
$category = $data['Category'];
$venue = $data['Venue'];
$events = EventPage::get()->filter(['Published' => true])->sort('EventDate DESC');
if (!empty($eventTitle)) {
$events = $events->filter(['EventTitle:PartialMatch' => $eventTitle]);
}
if (!empty($category) && $category != 'All') {
$events = $events->filter('Category', $category);
}
if (!empty($venue) && $venue != 'All') {
$events = $events->filter('Venue', $venue);
}
$paginatedEvents = PaginatedList::create($events, $request)
->setPageLength(12);
return [
'SearchResults' => $paginatedEvents,
];
}
}
My template EventSearchPage.ss:
<div class="search-container">
<h2>Find Events</h2>
$EventFilterForm
<% include EventSearchResults %>
</div>
And the results template EventSearchResults.ss:
<div class="results-section">
<% if $SearchResults %>
<div class="event-grid">
<% loop $SearchResults %>
<div class="event-item">
<h4><a href="$Link">$EventTitle</a></h4>
<p>Date: $EventDate.Format('dd/MM/yyyy')</p>
<p>Category: $Category</p>
<p>Venue: $Venue</p>
</div>
<% end_loop %>
</div>
<% else %>
<p>No events found matching your criteria.</p>
<% end_if %>
</div>
The search form works but no results show up in the template. What am I missing here?