2

I also used dpm($form_state->getValue('input_select','text')); and dpm($form_state->getValue('input_select','body'));its after the ajax handler is called...

what is the right way to fetch value for grouped form fields in drupal 8 ? and I need to achieve this programmatically not via modules

<?php

namespace Drupal\Mymodule\Form;

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

class Untitled extends FormBase
{
    public function getFormId()
    {
        return 'Mymodule_test_form';
    }

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

        $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title of the field'),
    '#description' => t('Enter the Desired title'),
    '#size' => 60,
    '#required' => true,
     '#default' => ($form_state->isValueEmpty('title')) ? null : $form_state->getValue('title'),
  );

        $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#description' => t('Add or edit'),
    '#options' => array_combine(array(t('Add'), t('Edit')), array(t('Add'), t('Edit'))),
    '#default' => ($form_state->isValueEmpty('type')) ? 'Add' : $form_state->getValue('type'),
    '#required' => true,
    '#ajax' => array(
      'callback' => '::ajax_handler',
      'wrapper' => 'input-fields',
    ),

  );

        $form['input_select'] = array(
    '#type' => 'container',
    '#attributes' => ['id' => 'input-fields'],
  );

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

        return $form;
    }

    public function ajax_handler(array &$form, FormStateInterface $form_state)
    {
        if (($form_state->getValue('type')) === ('Add')) {
            $form['input_select']['text'] = array(
          '#type' => 'textfield',
          '#title' => t('Title of the add Block'),
          '#description' => t('Enter the Desired title'),
          '#size' => 60,
          '#required' => true,
          //how to get value of field text if add is selected
           '#default' => ($form_state->isValueEmpty('text')) ? null : $form_state->getValue('text'),
        );
        }

        if ($form_state->getValue('type') === ('Edit')) {
            $form['input_select']['body'] = array(
            '#type' => 'textarea',
            '#title' => t('Title of the edit Block'),
            '#description' => t('Enter the Desired feedback'),
            '#size' => 60,
            '#required' => true,
            //how to get value of field body if edit is selected
             '#default' => ($form_state->isValueEmpty('body')) ? null : $form_state->getValue('body'),
          );
        }

        return $form['input_select'];
    }
}

thank-you for your efforts

1
  • In your $form['type'] the ['#options'] array must be a key value. Sure you've done this, but translate a key is really necessary? Make sure when use === operator, sometimes its not needed. By the way check this answer (drupal.stackexchange.com/a/200201/54619), there is something like you asking (container that change its values when select change).
    – Vagner
    May 10, 2016 at 0:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.