function Simplest::buildForm
File
-
modules/ajax_example/src/Form/Simplest.php, line 23
Class
- Simplest
- A relatively simple AJAX demonstration form.
Namespace
Drupal\ajax_example\Form
Code
public function buildForm(array $form, FormStateInterface $form_state) {
$form['change_this'] = [
'#title' => $this->t("Choose something and explain why"),
'#type' => 'select',
'#options' => [
'one' => 'one',
'two' => 'two',
'three' => 'three',
],
'#ajax' => [
'callback' => '::promptCallback',
'wrapper' => 'replace-textfield-container',
],
];
$form['replace_textfield_container'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'replace-textfield-container',
],
];
$form['replace_textfield_container']['replace_textfield'] = [
'#type' => 'textfield',
'#title' => $this->t("Why"),
];
$value = $form_state->getValue('change_this');
if ($value !== NULL) {
$form['replace_textfield_container']['replace_textfield']['#description'] = $this->t("Say why you chose '@value'", [
'@value' => $value,
]);
}
return $form;
}