Form State Empty
This module enhances Drupal's conditional form fields by allowing you to clear the value of an input element when the state of another form element changes. This is especially useful for preventing unintended data submission for fields that are hidden or toggled by other form elements.
Provides a small extension to Core's conditional form fields system. Allows the clearing of an html input element based on the state of another form element(s).
Example:
$form['field_to_empty']['#states'] = [
'empty' => [
':input[name="field_being_listened_to"]' => ['value' => '0'],
],
'visible' => [
':input[name="field_being_listened_to"]' => ['value' => '1'],
]
];
The most common usage is when a field's visibility is being toggled. You don't want the value from an invisible field to be submitted. Before the best way to handle this was to "disable" the form element. Unfortunately this prevented that element from submitting at all so you could never overwrite a previous form submission.