0

I'm building a custom form and it display well. My form has a table type field which I can't manage to access from the submitForm method.

public function buildForm(array $form, FormStateInterface $form_state)
{
    $form['name'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Name'),
        '#required' => true,
    ];

    $form['template_components_list'] = [
        '#type' => 'table',
        '#caption' => $this
        ->t('My table.'),
        '#tree' => TRUE,
        '#header' => array(
            $this
            ->t('row_name'),
            $this
            ->t('description'),
            $this
            ->t('actions'),
        ),
    ];

    $form['template_components_list'][0]['row_name'] = array(
        '#plain_text' => 'Test1',
    );

    $form['template_components_list'][1]['row_name'] = array(
        '#plain_text' => 'Test2',
    );

    $form['actions'] = [
                '#type' => 'actions',
            ];
    // Add a submit button that handles the submission of the form.
    $form['actions']['submit'] = [
                '#type' => 'submit',
                '#value' => $this->t('Submit'),
            ];

    return $form;
}

I'm then trying to access the values in submitForm from multiple ways (some creating errors but I want to show I tried them)

public function submitForm(array &$form, FormStateInterface $form_state)
{
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r('START', TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->getValue('row_name'), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->get('row_name'), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->get('row_name')->getValue(), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->getValue('template_components_list'), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->get('template_components_list'), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($form_state->get('template_components_list')->getValue(), TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r('END', TRUE) . '</code></pre>');
}

}

I got only empty messages or errors in my logs

Error: Call to a member function getValue() on null

What is the good way to access my table rows values from submitForm ?

0

Sorry, I must have overlooked the way you built the form. Unless you make your values in the table row part of the form, you cannot submit them. I changed the row '#type' to a text field and added a default value, in that way you can now pass that along with your form. Try this:

public function buildForm(array $form, FormStateInterface $form_state)
{
    $form['name'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Name'),
        '#required' => true,
    ];

    $form['template_components_list'] = [
        '#type' => 'table',
        '#caption' => $this
        ->t('My table.'),
        '#tree' => TRUE,
        '#header' => array(
            $this
            ->t('row_name'),
            $this
            ->t('description'),
            $this
            ->t('actions'),
        ),
    ];

    $form['template_components_list'][0]['row_name'] = array(
        '#type' => 'textfield',
         '#default_value' => 'Test1'

    );

    $form['template_components_list'][1]['row_name'] = array(
              '#type' => 'textfield',
         '#default_value' => 'Test2'

    );

    $form['actions'] = [
                '#type' => 'actions',
            ];
    // Add a submit button that handles the submission of the form.
    $form['actions']['submit'] = [
                '#type' => 'submit',
                '#value' => $this->t('Submit'),
            ];

    return $form;
}



public function submitForm(array &$form, FormStateInterface $form_state)
{

   $SubmittedValues = $form_state->getValues();
   print_r($SubmittedValues);
   //Now you can access the form values as an associative array 
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r('START', TRUE) . '</code></pre>');
    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($SubmittedValues['row_name'], TRUE) . '</code></pre>');

    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r($SubmittedValues['template_components_list'], TRUE) . '</code></pre>');

    \Drupal::logger('some_channel_name')->warning('<pre><code>' . print_r('END', TRUE) . '</code></pre>');
}

  • Thanks for your help but it doesn't seems to work, I've made a "kint($SubmittedValues)" I see my table but it's empty – Hyco Sep 6 at 7:37
  • check the updated answer. – Hunde Tekle Sep 9 at 13:50
  • It does work thank you ! :) – Hyco Sep 10 at 14:03

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.