0

I am trying to throw back values that are calculated in submitForm() to buildForm().

How do I add more state values into it?

    /**
     * @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['number1'] = array(
          '#type' => 'textfield',
          '#title' => t('number 1'),
          '#required' => TRUE,
        );

        $form['number2'] = array(
          '#type' => 'textfield',
          '#title' => t('number 2'),
          '#required' => TRUE,
        );

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

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

        return $form;
      }


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

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

$total = $form_state->getValue('number1') * $form_state->getValue('number2');

    $form_state->setValue('total',array($total));

           }
        }
  • $form_state->setRebuild(); in the submit method? – Clive Aug 12 '16 at 12:47
  • not working at all tried it @Clive – Boby Aug 12 '16 at 13:29
0

You can add the following code:

/**
 * {@inheritdoc}
 */
  public function buildForm(array $form, FormStateInterface $form_state) {
     ...
     if (!empty($form_state->get('total')) {
       $form['total'] = array(
         '#type' => 'markup',
         '#markup' => '<p> Total is: <strong>' . $form_state->get('total') . '</strong></p>',
       );
     }
    //$header = array(
    //  'total',
    //);
    //$output = array();
    //foreach ($form_state->getValues() as $key => $value) {
    //   $output['total'] = $total;
    //}
    //$form['mytable'] = array(
    //  '#type' => 'table',
    //  '#header' => $header,
    //  '#rows' => $output,
    //  '#empty' => t('No Data'),
    //);
    return $form;
   }

Add the following code to your submit handler:

/**
 * {@inheritdoc}
 */
 public function submitForm(array &$form, FormStateInterface $form_state) {
   ...
    $total = $form_state->getValue('number1') * $form_state->getValue('number2');
    $form_state->set('total', $total);
    $form_state->setRebuild(TRUE);
    return $form
 }

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.