gpt4 book ai didi

php - SilverStripe 相关下拉列表 - x 不是有效选项

转载 作者:行者123 更新时间:2023-12-04 03:20:28 26 4
gpt4 key购买 nike

我有一个简单的下拉字段,其中包含 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();
}

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

Please select a value within the list provided. x is not a valid option

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

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

最佳答案

看起来您只需要为您的 Map 对象创建一个 Array

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

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

关于php - SilverStripe 相关下拉列表 - x 不是有效选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38698291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com