0

I am testing out the new Drupal 8 custom modules and currently stuck with the submit form handler to load form submitted results in the same page and my current functions of last submit is this (inside the controller form) :

    <?php
/**
 * @file
 * Contains \Drupal\resume\Form\ResumeForm.
 */
namespace Drupal\resume\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class ResumeForm extends FormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'resume_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['candidate_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Candidate Name:'),
      '#required' => TRUE,
    );

    $form['candidate_mail'] = array(
      '#type' => 'email',
      '#title' => t('Email ID:'),
      '#required' => TRUE,
    );

    $form['candidate_number'] = array (
      '#type' => 'tel',
      '#title' => t('Mobile no'),
    );

    $form['candidate_dob'] = array (
      '#type' => 'date',
      '#title' => t('DOB'),
      '#required' => TRUE,
    );

    $form['candidate_gender'] = array (
      '#type' => 'select',
      '#title' => ('Gender'),
      '#options' => array(
        'Female' => t('Female'),
        'male' => t('Male'),
      ),
    );

    $form['candidate_confirmation'] = array (
      '#type' => 'radios',
      '#title' => ('Are you above 18 years old?'),
      '#options' => array(
        'Yes' =>t('Yes'),
        'No' =>t('No')
      ),
    );

    $form['candidate_copy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send me a copy of the application.'),
    );

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#button_type' => 'primary',
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
    public function validateForm(array &$form, FormStateInterface $form_state) {

      if (strlen($form_state->getValue('candidate_number')) < 10) {
        $form_state->setErrorByName('candidate_number', $this->t('Mobile number is too short.'));
      }

    }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

   // drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('candidate_name'))));

    foreach ($form_state->getValues() as $key => $value) {
      drupal_set_message($key . ': ' . $value);
    }

   }
}

I wanted to show results in a table in the same page form where user submit the form, rather than just throwing in messages

thank you

  • Above code working fine for me.. Kindly post complete code to check throughly.. – Ajay Reddy Aug 9 '16 at 7:19
  • @AAjayKumarReddy updated form – Boby Aug 9 '16 at 13:37
1

The example of @Ajay was not working for me. This one do. Hope this help.

public function buildForm(array $form, FormStateInterface $form_state) {

    // Your $form fields here ...

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Save'),
      '#button_type' => 'primary',
    );

    // Render this field bellow the submit button and above return $form.
    if (!empty($formValues = $form_state->getValues())){

      $header = ["Key","Value"];

      $output = [];
      foreach ($formValues as $key => $value) {
        if ($key=='submit'){
          break;
        }
        $output[$key]['key'] = $key;
        $output[$key]['value'] = $value;
      }

      $form['mytable'] = [
        '#type' => 'table',
        '#header' => $header,
        '#rows' => $output,
        '#empty' => t('No Data'),
      ];
    }

    return $form;
}

public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->setRebuild();
}

Soon I'll write a post about other ways to render form's data in a controller or even with a Private Temp Storage

  • Thanks for this solution it works like charm, Can you help on how can i get checkbox in $output[$key]['key'] if I try adding html it just loads plain html – Ekta Puri May 16 at 6:23
0

After submit check for

     if(isset($form_state->getValues())){
        $header = array(
          array('key' => t('Key')),
          array('value' => t('Value')),
        );
        $output = array();
        foreach ($form_state->getValues() as $key => $value) {
           $output['key'] = $key;
           $output['value'] = $value;
        }
        $form['mytable'] = array(
          '#type' => 'table',
          '#header' => $header,
          '#rows' => $output,
          '#empty' => t('No Data'),
        )),
     }

in buildForm and try like this.

  • do I join like $form['asd']['#prefix'] = 'value'; as I need to build table into it thank you – Boby Aug 10 '16 at 10:40
  • Updated answer, try this once – Ajay Reddy Aug 10 '16 at 11:00
  • not working, as I checked the form_state are empty once its submitted, it is not returning ot the build form? – Boby Aug 12 '16 at 9:41
  • $input = &$form_state->getUserInput(); Once try this, but this will give your table even if validation is there. – Ajay Reddy Aug 12 '16 at 9:56
  • I am processing the input and resubmitting as another variable is it possible to send arguments or value back to the storage? like in submitForm : form_state->get('value1') * form_state->get('value2'); – Boby Aug 12 '16 at 11:58

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.