KenSander
KenSander

Reputation: 113

3.1 Search DataObjects & Select Fields

Since upgrading to 3.1 I'm noticing that the select fields returned on a search form for dataobjects for enum values don't return the default 'any' option, but rather the first value. This makes it so users have to search a specific value for that field, rather than 'any'.

in dataobject.php

public function getCustomSearchContext() {
    $fields = $this->scaffoldSearchFields(array(
        'restrictFields' => array('Field1', 'Field1')
    ));

    $filters = array(
        'SomeField' => new PartialMatchFilter('Field1'),
                 .....etc
    );

    return new SearchContext(
        $this->class, 
        $fields, 
        $filters
    );
}

SomePage.php

public function DOSearch() {
    $context = singleton('DataObject')->getCustomSearchContext();
    $fields = $context->getSearchFields();

    $form = new Form($this, "DOSearch",
        $fields,
        new FieldList(
            new FormAction('doDOSearch')
        )
    );
    return $form;
}


public function doDOSearch($data, $form) {

    $context = singleton('DataObject')->getCustomSearchContext();
    $set = ArrayList::create( $context->getResults($data)->toArray() );

    return $this->customise(array(
        'Set1' => $Set1
    ))->renderWith(array('DOResults', 'Page'));
}

The dataobject is setup with the Enum column and $searchable_fields set. I want users to have the option for 'any' instead of having to pick 1 of the set values that gets returned.

Upvotes: 1

Views: 996

Answers (2)

Nico Haase
Nico Haase

Reputation: 12105

This is a bug in SilverStripe, which is fixed through https://github.com/silverstripe/silverstripe-framework/pull/2566 and will hopefully make its way into the core soon

Upvotes: 0

colymba
colymba

Reputation: 2644

You should be able to edit the DropdownField in the getCustomSearchContext() function after you scaffold $fields with either:

$fields->fieldByName('TheNameOfTheDropdownField')->setHasEmptyDefault(true);

this will make it possible to clear the selection or:

$fields->fieldByName('TheNameOfTheDropdownField')->setEmptyString('Any');

which will add an 'Any' empty option to the field.

Upvotes: 1

Related Questions