The States (Form) system
The last thing we are going to look at in this chapter is the States system of the Form API (not to be confused with the State API we covered in Chapter 6, Data Modeling and Storage). This allows us to define our form elements to behave somewhat dynamically based on the user interaction with the form. It doesn’t use Ajax but relies on JavaScript to handle the manipulations. This is another great example of client-side behavior where we don’t have to write a single line of JavaScript. So, let’s see what this is.
The #states
are simple properties we can add to form elements, which have the role of changing them depending on the state of other elements. The best way to understand this is through some examples. Imagine these two form elements:
$form['kids'] = [ '#type' => 'checkbox', '#title' => $this->t('Do you have kids?'), ]; $form['kid_number&apos...