Same name and namespace in other branches
- 8.9.x core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php \Drupal\form_test\Form\FormTestValidateRequiredForm::buildForm()
- 9 core/modules/system/tests/modules/form_test/src/Form/FormTestValidateRequiredForm.php \Drupal\form_test\Form\FormTestValidateRequiredForm::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- core/
modules/ system/ tests/ modules/ form_test/ src/ Form/ FormTestValidateRequiredForm.php, line 25
Class
- FormTestValidateRequiredForm
- Form constructor to test the #required property.
Namespace
Drupal\form_test\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$options = [
'foo' => 'foo',
'bar' => 'bar',
];
$validate = [
'::elementValidateRequired',
];
$form['textfield'] = [
'#type' => 'textfield',
'#title' => 'Name',
'#required' => TRUE,
'#required_error' => t('Enter a name.'),
];
$form['checkboxes'] = [
'#type' => 'checkboxes',
'#title' => 'Checkboxes',
'#options' => $options,
'#required' => TRUE,
'#form_test_required_error' => t('Choose at least one option.'),
'#element_validate' => $validate,
];
$form['select'] = [
'#type' => 'select',
'#title' => 'Select',
'#options' => $options,
'#required' => TRUE,
'#form_test_required_error' => t('Select something.'),
'#element_validate' => $validate,
];
$form['radios'] = [
'#type' => 'radios',
'#title' => 'Radios',
'#options' => $options,
'#required' => TRUE,
];
$form['radios_optional'] = [
'#type' => 'radios',
'#title' => 'Radios (optional)',
'#options' => $options,
];
$form['radios_optional_default_value_false'] = [
'#type' => 'radios',
'#title' => 'Radios (optional, with a default value of FALSE)',
'#options' => $options,
'#default_value' => FALSE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
];
return $form;
}