Set the message field as not required in a custom contact form I have tried many things to set the message field as not required.

1-The more obvious in form_alter

$form['message']['widget'][0]['value']['#required']=false;
$form['message']['widget'][0]['#required']=false;
$form['message']['widget']['#required']=false;
$form['message']['#required']=false;


2-Set a white space as a value (if field is empty) in form_validate

$msg= $form_state-&gt;getValue('message');
if(!isset($msg['value'])){
    $form_state-&gt;setValue('message', 
        [
            'value' =&gt; '  '
        ]
    );
}


3-Set not required in after build

function alter_contact_ds(&amp;$form, FormStateInterface $form_state, $form_id)
{
    $form['#after_build'][] = '_remove_msg_required';
    ...
}
function _remove_msg_required($element){
    $element['message']['widget'][0]['value']['#required']=false;
    $element['message']['widget'][0]['#required']=false;
    $element['message']['widget']['#required']=false;
    $element['message']['#required']=false;
    return $element;
}


4- In form_alter, I have used the #state to set the required property

$form['message']['#states'] = ['required' =&gt; ['select[id="edit-object"]' =&gt; [
    'value' =&gt; 'info'
]]];
//Note that ['select[id="edit-object"]' =&gt; ['value' =&gt; 'info']]]
//is used succesfully on another field (but not the requied part)


The best I could get is not having the regular behavior explaining that the field must be completed (and no red asterix near the field to mark it as required)

But each time I press on the submit button, I get a drupal_set_message("This value could not be null",error) [freely translated from french]

SOLUTION:

Following the idea of Alireza Tabatabaeian, I could solve this problem by

1-Set the field as not required in form alter

$form['message']['widget'][0]['value']['#required'] = false;


2-Set my validation function de first one in form_alter

$form['#validate'] = ['my_validate_contact','::validateForm'];


3-In my validate function set the value of the field equal to a space if the field is empty (it is not really nice, but I could accept it)

$msg = $form_state-&gt;getValue('message');
if (! isset($msg['value'])) {
    $form_state-&gt;setValue('message', [
        [
            'value' =&gt; '  '
        ]
    ]);
}
I've solved this issue this way:

function hook_entity_base_field_info_alter(&amp;$fields, EntityTypeInterface $entity_type) {
  if ($entity_type-&gt;id() == 'contact_message') {
    if (isset($fields['message']) &amp;&amp; $fields['message'] instanceof BaseFieldDefinition) {
      /* @var BaseFieldDefinition $field */
      $field = &amp;$fields['message'];
      $field-&gt;setRequired(FALSE);
    }
  }
}