0

I am setting up a userform and I want to dropdown field which gets its values from an external API. The dropdown field get the values and when I add it to the userform, the form submission goes to 500: Error. I was wondering if there is a procedure for this in 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
    Could you provide the exact error message you're receiving? – Cheddam May 29 '19 at 23:31
  • Hi @Cheddam, It doesnt show any error even in the dev mode but just shows a white screen. The URL after form submission is /contact-us/user-defined-form/Form instead of /contact-us/new-user-defined-form/#success which is the confirmation for submission. It happens only when this field is added to the form. – Vijay Adhithyan Mohan May 30 '19 at 00:22
  • The first thing you always do on a 500, is you go check the server’s error log. – 04FS Jun 04 '19 at 07:31
  • Possible duplicate of https://stackoverflow.com/questions/1475297/phps-white-screen-of-death (based on _“but just shows a white screen”_) – 04FS Jun 04 '19 at 07:32
  • Smoothly done. Thanks – Vijay Adhithyan Mohan Jun 06 '19 at 21:36

1 Answers1

0

The form action lead to white screen of death which is at this instance didn't create a log. So we figured out that this is to do with Silverstripe Userforms not handling the data from an external APi. My last resort was update to the latest version which fixed this issue. Thanks everyone.