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.phpin thesrc/Formdirectory for your module to hold theApprovalRequiredFormform class. - We will define the
ApprovalRequiredFormclass 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...