Limiting number of multiples in a drupal webform in an ajax callback I have a form with a select element to choose the number of guests you want to add to an event.  This select has an ajax callback that updates a container that holds a contact composite element.  

I am trying to in my ajax callback update the number of multiple contact elements to the value of the number of guests select box.  Anyone have any thoughts?  If I use a form alter like below on initial load the number of elements updates correctly.

My alter attempting to just set multiple number outside ajax

function lebow_events_webform_submission_form_alter(array &amp;$form, FormStateInterface $form_state, $form_id) {
  $form['elements']['guest_info']['guest_contact']['#type'] = 'webform_multiple';
  $form['elements']['guest_info']['guest_contact']['#webform_multiple'] = TRUE;
  $form['elements']['guest_info']['guest_contact']['#multiple'] = 10;
  $form['elements']['guest_info']['guest_contact']['#cardinality'] = 10;
  $form['elements']['guest_info']['guest_contact']['#sorting'] = FALSE;
}


My webform source

registration_type:
  '#type': select
  '#title': 'Registration Type'
  '#options': registration_types
guests_container:
  '#type': container
  '#states':
    invisible:
      ':input[name="registration_type"]':
        value: ''
  number_of_guests:
    '#type': select
    '#title': 'Number of guests'
    '#options':
      - '0'
guest_info:
  '#type': container
  '#title': 'Guest Info'
  guest_contact:
    '#type': contact
    '#title': 'Contact Info'
    '#multiple': 2
    '#multiple__add_more': '2'
    '#company__access': false
    '#phone__access': false
    '#address__access': false
    '#address_2__access': false
    '#city__access': false
    '#state_province__access': false
    '#postal_code__access': false
    '#country__access': false


My alter to add ajax callback (and try setting number of multiples)

function lebow_events_webform_submission_form_alter(array &amp;$form, FormStateInterface $form_state, $form_id) {

  $form['elements']['guests_container']['number_of_guests']['#options'] = ['' =&gt; '- None -'];
  $form['elements']['registration_type']['#ajax'] =
    array(
      'callback' =&gt; 'lebow_events_registration_change',
      'wrapper' =&gt; 'edit-guests-container',
    );
  $form['elements']['guests_container']['number_of_guests']['#ajax'] =
    array(
      'callback' =&gt; 'lebow_events_guests_change',
      'wrapper' =&gt; 'edit-guest-info',
    );

  $form['elements']['guests_container']['number_of_guests']['#validated'] = TRUE;
  $form['elements']['guest_info']['guest_contact']['#type'] = 'webform_multiple';
  $form['elements']['guest_info']['guest_contact']['#webform_multiple'] = TRUE;
  $form['elements']['guest_info']['guest_contact']['#multiple'] = 10;
  $form['elements']['guest_info']['guest_contact']['#cardinality'] = 10;
  $form['elements']['guest_info']['guest_contact']['#sorting'] = FALSE;
}


My ajax callback

function lebow_events_guests_change(&amp;$form, \Drupal\Core\Form\FormState $form_state) {

  $form['elements']['guest_info']['guest_contact']['#type'] = 'webform_multiple';
  $form['elements']['guest_info']['guest_contact']['#webform_multiple'] = TRUE;
  $form['elements']['guest_info']['guest_contact']['#multiple'] = $form_state-&gt;getValue('number_of_guests');
  $form['elements']['guest_info']['guest_contact']['#cardinality'] = $form_state-&gt;getValue('number_of_guests');
  $form['elements']['guest_info']['guest_contact']['#sorting'] = FALSE;
  $form['elements']['guest_info']['guest_contact']['#element']['#webform_multiple'] = TRUE;
  $form['elements']['guest_info']['guest_contact']['#element']['#multiple'] = $form_state-&gt;getValue('number_of_guests');

  $message = $form['elements']['guest_info'];

  $response = new \Drupal\Core\Ajax\AjaxResponse();
  $response-&gt;addCommand(new \Drupal\Core\Ajax\HtmlCommand('[data-drupal-selector=edit-guest-info]', $message));

  return $response;

}
It is very difficult and sometimes impossible to alter a form via an Ajax callback.  Generally, an Ajax callback just returns the updated form element from the $form render array.

The Examples for Developers module includes several Ajax form examples that you can use as a starting point.