1

自从升级到 3.1 后,我注意到在搜索表单上返回的用于枚举值的数据对象的选择字段不返回默认的“任何”选项,而是返回第一个值。这使得用户必须搜索该字段的特定值,而不是“任何”。

在 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'));
}

数据对象设置为 Enum 列和 $searchable_fields 集。我希望用户可以选择“任何”,而不必选择返回的设置值中的 1 个。

4

2 回答 2

1

在使用以下任一方法搭建脚手架后,您应该能够DropdownField在函数中编辑:getCustomSearchContext()$fields

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

这将可以清除选择或:

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

这将为该字段添加一个“任何”空选项。

于 2013-10-16T17:03:15.067 回答
0

这是 SilverStripe 中的一个错误,已通过https://github.com/silverstripe/silverstripe-framework/pull/2566修复,并有望很快进入核心

于 2013-10-19T13:19:35.640 回答