I’m creating a custom form with a file upload field that will have its contents read, processed then sent to an external API. The problem is whenever I upload the file the field for it appears to be empty in the submit handler and I cannot see why.
In this example I’m using it as an unmanaged file as I only need to process it then remove it but I have tried it as a managed file which does upload (I can see it in the directory) and has the file added to the file_managed database table. I’m not sure where unmanaged file are meant to go to check for their upload.
public function buildForm(array $form, FormStateInterface $form_state) { $form = [ '#attributes' => ['enctype' => 'multipart/form-data'], ]; $form['file_upload_details'] = [ '#markup' => "<strong>" . $this->t('The KML File') . "</strong>", ]; $form['my_file'] = [ '#type' => 'file', '#description' => $this->t('KML format only'), '#upload_location' => 'public://my_files/', '#upload_validators' => [ 'file_validate_extensions' => ['kml'], ], ]; $form_build = parent::buildForm($form, $form_state); return $form_build; }
public function submitForm(array &$form, FormStateInterface $form_state) { $messenger = parent::messenger(); var_dump( $form_state->getStorage(), // Empty array $form_state->getValue('my_file') // Empty string ); }