2016-08-01 53 views
2

我有2个值和相关下拉字段一个简​​单的下拉字段:SilverStripe依赖下拉 - x不是一个有效的选项

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例如它说“1不是有效的选项”等每个区域对象。它被卡在验证中,甚至没有进入操作。我在网站/其他网站的其他部分做了类似的事情,并且他们工作得很好。

为什么此验证不正确地阻止表单提交,我们该如何解决这个问题?

回答

1

好像你只需要创建一个Map对象Array

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

通常情况下,你可以只使用Map对象为源的DropdownField。但我认为DependentDropdownFieldMap对象有一些问题。

相关问题