我有一个简单的下拉字段,有两个值和一个相关的下拉字段:
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不是有效选项"等。它在验证时被卡住了,甚至没有付诸行动。我在网站的其他部分/其他网站做过类似的事情,它们运行得很好。
为什么此验证错误地阻止了表单提交?我们如何解决此问题?
似乎只需要创建Map
对象的Array
。
if ($val =='yes') {
$areas = Area::get()->filter('ParentID', '0');
return $areas->map('ID', 'Name')->toArray();
}
通常,您可以只使用Map
对象作为DropdownField
的源。但我认为DependentDropdownField
在Map
对象上有一点问题。