如何将空值添加到Silverstripe DropDownField并要求选择非空值?

时间:2015-02-13 03:46:03

标签: select silverstripe

如何将空值添加到Silverstripe DropDownField并要求选择非空值?

目前,我的领域是:

$myField = new DropdownField(
  'MyField',
  "So what'll it be, yes or no?",
  array( "N"=>"No", "Y"=>"Yes")
);

使用SilverStripe 3.1

1 个答案:

答案 0 :(得分:2)

您想要设置

$myField->setHasEmptyDefault(true);

然后使用控制器中的Form validation

类似的东西:

public function myForm() {
    $myField = new DropdownField(
        'MyField',
        "So what'll it be, yes or no?",
        array( "N"=>"No", "Y"=>"Yes")
    );
    $fields = new FieldList($myField);
    $validator = new RequiredFields('MyField');

    return new Form($this, 'myFormForm', $fields, $actions, $validator);
}