【问题标题】:SilverStripe dependent dropdown - x is not a valid optionSilverStripe 相关下拉列表 - x 不是有效选项
【发布时间】:2016-08-01 11:57:43
【问题描述】:

我有一个包含 2 个值的简单下拉字段和一个依赖下拉字段:

public function areaForm() {
    $datasource = function($val) {
        if ($val =='yes') {
            $areas = DataObject::get('Area', 'ParentID = 0');
            return $areas->map('ID', 'Name');
        }
        if ($val == 'no') {
            return false;
        }
    };

    $fields = new FieldList(
        TextField::create('Name', 'Area Name:'),
        $dropField = DropdownField::create('isChild', 'Is this a sub Area?', array('yes' => 'Yes', 'no'=>'No' ))
            ->setEmptyString('Select one'),
        DependentDropdownField::create('ParentSelect', 'Select Parent Area:', $datasource)
            ->setDepends($dropField)
            ->setEmptyString('Select one')
    );

    return new Form($this, __FUNCTION__, $fields, FieldList::create(new FormAction('doSaveArea', 'Save area')));
}

public function doSaveArea($data, $form) {
    var_dump($data);
    exit;
    $name = $data['Name'];
    $isChild = $data['isChild'];

    if ($isChild === 'no') {
        $area = new Area();
        $area->Name = $name;
        $area->ParentID = 0;
        $area->write();
    }
    elseif ($isChild === 'yes') {
        $area = new Area();
        $area->Name = $name;
        $area->ParentID = $data['ParentSelect'];
        $area->write();
    }
    $this->redirectBack();
}

每当我尝试通过提交表单来保存我的对象时,它都会给我同样的信息:

请在提供的列表中选择一个值。 x 不是一个有效的选项

值被正确填充。我可以通过检查元素在浏览器中看到它们。然而,如果我选择 ID 1,例如它会为每个 Area 对象显示 “1 不是有效选项” 等。它卡在验证中,甚至没有采取行动。我在网站/其他网站的其他部分做过类似的事情,它们工作正常。

为什么这个验证会错误地阻止表单提交,我们如何解决这个问题?

【问题讨论】:

    标签: php silverstripe


    【解决方案1】:

    似乎您只需要为您的Map 对象创建一个Array

    if ($val =='yes') {
        $areas = Area::get()->filter('ParentID', '0');
        return $areas->map('ID', 'Name')->toArray();
    }
    

    通常您可以只使用Map 对象作为DropdownField 的源。但我认为DependentDropdownFieldMap 对象有点问题。

    【讨论】:

      猜你喜欢
      • 2019-07-13
      • 1970-01-01
      • 2019-02-26
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 2014-09-13
      相关资源
      最近更新 更多