Score:0

Ajax callback not returning options for checkboxes

pl flag

I'm triggering an ajax callback on change event of a dropdown, this method returns additional form elements like text fields, number and checkboxes, but for checkboxes the options are not appearing at all.

---------
Container for additional elements

  $form['dynamic_field_container'] = [
                '#type' => 'container',  
                '#attributes' => ['id' => 'dynamic-field-wrapper'],
              ];

----------

   public function ProjectOptions($data, $ProjectOptions)
    {
        return [
            '#type' => 'select',
            '#options' => $data,
            '#title' => t('Options Name'),
            '#required' => TRUE,
            '#default_value' => $ProjectOptions,
     
            '#ajax' => [
              'callback' => [$this,'dynamicFieldCallback'],
              'wrapper' => 'dynamic-field-wrapper',
              'event' => 'change',
              'progress' => [
                'type' => 'throbber',
                'message' => t('Loading...'),
              ],
            ],
        ];
    }

    public function dynamicFieldCallback(array &$form, FormStateInterface $form_state) {
            $form['dynamic_field_container']['number_field'] = [
                    '#type' => 'number',
                    '#title' => t('Number field titile'),
                    '#description' => t('This field changes based on dropdown selection.'),
                    '#required' => TRUE,
                    '#value' =>'555'
                  ];
            $options = [
                        'option1' => 'Option 1',
                        'option2' => 'Option 2',
                        'option3' => 'Option 3',
                ];
            $form['dynamic_field_container']['dynamic_field_checkbox' ] = [
                    '#type' => 'checkboxes',
                    '#title' => t('Titile of the Check box field'),
                    '#description' => t('This field changes based on dropdown selection.'),
                    '#required' => TRUE,
                    '#options' => $options,
                    '#value' => true
                ];
        return $form['dynamic_field_container'];
    } 

In the output, only the title of checkboxes are visible

4uk4 avatar
cn flag
See https://drupal.stackexchange.com/questions/314435/dynamicaly-add-ajax-form-item-and-add-event-on-checkbox
Himanshu Lahare avatar
pl flag
Thanks! By calling the processCheckboxes, processAjaxForm i was able to render the options but I'm still unable to set either the '#value' or '#default_value' to the checkboxes element.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.