在用户表单中添加一个下拉字段,以填充API中的数据

时间:2019-05-29 22:02:26

标签: php silverstripe

我正在设置一个用户窗体,我想下拉字段,该字段从外部API获取其值。下拉字段获取值,并将其添加到用户表单时,表单提交将转到500:错误。我想知道Silverstripe中是否有此程序

class TestDropdown extends EditableMultipleOptionField {

private static $singular_name = 'Test Dropdown Field';

private static $plural_name = 'Test Dropdown Fields';

/**
 * @return DropdownField
 */
public function getFormField() {    
    function testAPICall($method, $url, $data){
       $curl = curl_init();

       switch ($method){
          case "POST":
             curl_setopt($curl, CURLOPT_POST, 1);
             if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
             break;
          case "PUT":
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
             if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
             break;
          default:
             if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
       }

       // OPTIONS:
       curl_setopt($curl, CURLOPT_URL, $url);
       curl_setopt($curl, CURLOPT_HTTPHEADER, array(
          'api.authorization.key: xxxxxxx-yyyy-zzzz-aaaa-bbbbbbbbb',
          'Content-Type: application/json',
       ));
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

       // EXECUTE:
       $result = curl_exec($curl);
       if(!$result){die("Connection Failure");}
       curl_close($curl);
       return $result;
    }
    $get_data = testAPICall('GET', 'http://xxxxxx/yyy/zzzz', false);
    $optionSet =  json_decode($get_data, true);

    if($optionSet) {
        foreach($optionSet as $key => $value) {
            $options[$key] = $value;
        }
    }

    $field = DropdownField::create($this->Name, $this->Title, $optionSet);


    return $field;
}
}

1 个答案:

答案 0 :(得分:0)

表单动作导致白屏死亡,这是在此情况下没有创建日志。因此,我们发现这与Silverstripe用户窗体不处理来自外部APi的数据有关。我的最后一招是将其更新为最新版本,从而解决了此问题。谢谢大家。