【发布时间】: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