0

I want to know how i can access my submitted POST values from a form into a controller class. Do I need to access the $form_state variable or the request?

I am using the following code.

<?php

namespace Drupal\ps_response\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ps_response\Services\DefaultService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class FilterForm.
 *
 * @package Drupal\ps_response\Form
 */
class FilterForm extends FormBase {

  private $indexGenerator;

  public function __construct(DefaultService $indexGenerator) {
    $this->indexGenerator = $indexGenerator;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('ps_response.default')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'filter_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['survey_id'] = array(
      '#type' => 'select',
      '#title' => t('Survey'),
      '#required' => FALSE,
      '#options' => $this->indexGenerator->selectAllSurveys(),
      "#empty_option" => 'Selecteer Vragenlijst'
    );


    $form['question_id'] = array(
      '#type' => 'select',
      '#title' => t('Question'),
      '#required' => FALSE,
      '#options' => $this->indexGenerator->selectAllQuestions(),
      "#empty_option" => 'Selecteer Vraag in vragenlijst'
    );


    $form['pole_id'] = array(
      '#type' => 'select',
      '#title' => t('Pole'),
      '#required' => FALSE,
      '#options' => $this->indexGenerator->selectAllPoles(),
      "#empty_option" => 'Selecteer paal'
    );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    drupal_set_message(t('Zoekopdracht voltooid.'));
    // Do i need to return values here?
  }

}

My routing file :

ps_response.index_controller_index:
  path: '/ps_response/admin/dashboard'
  defaults:
    _controller: '\Drupal\ps_response\Controller\indexController::index'
    _title: 'Dashboard'
  requirements:
    _permission: 'access content'

ps_response.dummy_responses_dummy_responses:
  path: '/admin/content/polestarr/response/dummy-responses'
  defaults:
    _controller: '\Drupal\ps_response\Controller\DummyResponses::dummyResponses'
    _title: 'Dummy responses'
  requirements:
    _permission: 'access content'

ps_response.import_response_csv_files_import_response_csv_files:
  path: '/admin/content/polestarr/response/import-response-csv-files'
  defaults:
    _controller: '\Drupal\ps_response\Controller\ImportResponseCsvFiles::importResponseCsvFiles'
    _title: 'Import response CSV files'
  requirements:
    _permission: 'access content'

ps_response.index_controller_index_questions:
  path: '/ps_response/admin/dashboard/questions/request'
  defaults:
    _controller: '\Drupal\ps_response\Controller\indexController::selectQuestionsBySurvey'
  requirements:
    _permission: 'access content'

ps_response.filter.form:
  path: '/ps_response/Controller/indexController::index'
  defaults:
    _form: '\Drupal\ps_response\Form\FilterForm'
    _title: 'Conribute page'
  requirements:
    _permission: 'access content'

And this is my funciton inside of my controller :

public function index(Request $request) {
    $form = \Drupal::formBuilder()
      ->getForm('Drupal\ps_response\Form\FilterForm');


    $questionsByName = $this->indexGenerator->selectAllQuestions();
    $polesByName = $this->indexGenerator->selectAllPoles();
    $surveysByName = $this->indexGenerator->selectAllSurveys();
    $form_state = new FormState();
    $form_state->setRebuild();
    $form_state->getFormObject();


    /*
     * HOW DO I GET MY POST VALUES OVER HERE???????? 
     */

    if (!empty($_POST)) {
      $aList = $this->indexGenerator->selectResponses();

      $rating_totals = [];
      foreach ($aList as $row) {
        $rating = $row->option_value;
        $question_id = $row->question_id;
        if (!isset($rating_totals[$question_id])) {
          $rating_totals[$question_id] = [
            1 => 0,
            2 => 0,
            3 => 0,
            4 => 0,
            5 => 0,
            6 => $row->field_question_value
          ];
        }
        if (!isset($rating_totals[$question_id][$rating])) {
          $rating_totals[$question_id][$rating] = 0;
        }
        $rating_totals[$question_id][$rating]++;
      }

      return array(
        '#theme' => 'index',
        '#result' => $aList,
        '#ratings' => $rating_totals,
        '#request' => $_POST,
        '#questions' => $questionsByName,
        '#poles' => $polesByName,
        '#surveys' => $surveysByName,
      );
    }
    else {
      return array(
        '#theme' => 'index',
        '#questions' => $questionsByName,
        '#poles' => $polesByName,
        '#surveys' => $surveysByName,
        '#form' => $form
      );
    }
  }
2

If you redirect to a controller this is a new request. But then the user input from the last request is lost. You have to do something useful with the user input while you are in submit, for example store the data in the database or some kind of temporary storage like a session. Then the controller can access the data from there.

Btw. the drupal_set_message() you have in submit uses a session to store the message for the next request.

If you don't want to store the data and only want to display a result, it might be a better solution to rebuild the form and do this in form build.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.