Specifying conditional form elements
The Form API provides a mechanism for defining form element states. These states are mapped to JavaScript interactions that can control whether an element is required, visible, and more. In this example, we will demonstrate a form that has a disabled submit button until a checkbox is checked.
How to do it…
- Create a file named
ApprovalRequiredForm.php
in thesrc/Form
directory for your module to hold theApprovalRequiredForm
form class. - We will define the
ApprovalRequiredForm
class with a form ID ofmymodule_approval_form
:<?php
namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class ApprovalRequiredForm extends FormBase {
public function getFormId() {
return 'mymodule_approval_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
return...