$form_state->setError($element, t('Error message.'));
if($name == ''){ $form_state->setErrorByName('name', t('Name is required.')); }In this example, setErrorByName is used to set an error message for the form element 'name' if the $name variable is empty. These examples are taken from Drupal's official documentation and are used for demonstration purposes. The FormStateInterface class is part of the Drupal core module and is included in the main library of the Drupal package.