Skip to content

Required

A required child must be filled in every row. The module does not add a blank row when the default values already fill the list, because a blank row would fail validation.

$form['names'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Names'),
'#element' => [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
],
'#default_value' => ['One', 'Two', 'Three'],
];

A list of three rows. Each input has the label Name and a star that marks it as required.

#required => TRUE on the element asks for at least one non-empty row. It also sets #min_items to 1.

$form['names'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Names'),
'#required' => TRUE,
];

Submit the form with an empty list to see the error.

The form after a submit with an empty required list. The error message is at the top, and the empty row of the list is outlined in red.

$form['names']['#required_error'] = $this->t('Add at least one name.');
  • A required child inside an element with #states is required only while the element is visible.
  • Related properties: #required, #min_items.