How add conditional required field Need:

If we select a value in a drop-down list field(A), then we need to display and set as required field(B).

Note: the field(B) is a date range.

I tried to add states with a hook form alter 

$form['field_b']['#states'] = [
      'visible' =&gt; [
        ':input[name="field_a"]' =&gt; ['!value' =&gt; '_none']
      ],
      'required' =&gt; [
        ':input[name="field_a"]' =&gt; ['!value' =&gt; '_none'],
      ],
  ];


As you see I use: '!value' to negate a value and '_none' for no value. So, It will not be required if there is no value selected

This code didn't work. It display the field B, but it is not mandatory.

We found that there is an issue with core
https://www.drupal.org/project/drupal/issues/2855139

At the end issue was closed, but it didn't work for us. It worked with texte fields but not for our rage date.
So, if dont want to use conditional_fields module and only conditional fields core
You can do it, but required field have sometime a little bug.

In the issue someone take some code from conditional_fields that fix this issue, and use it. 

your_module/js/conditional_fields.js

(function ($, Drupal) {

// Fix core's required handling.
$(document).bind('state:required', function (e) {
  if (e.trigger) {
    var fields = $(e.target).find('input, select, textarea, fieldset');
    fields.each(function() {
      var label = 'label' + (this.id ? '[for=' + this.id + ']' : '');
      var $field_labels = $(e.target).find(label);
      var $fieldset_legends = $(e.target).find('legend span.fieldset-legend');
      var $labels = $field_labels.add($fieldset_legends);
      if (e.value) {
        $(this).attr({ required: 'required', 'aria-required': 'aria-required' });
        $labels.each(function() {
          $(this).addClass('js-form-required form-required');
        });
      } else {
        $(this).removeAttr('required aria-required');
        $labels.each(function() {
          $(this).removeClass('js-form-required form-required');
        });
      }
    })
  }
});

})(jQuery, Drupal);


In your_module.module in a form alter add: 

$form['#attached']['library'][] = 'your_module/conditional_fields';

$form['field_b']['#states'] = [
      'visible' =&gt; [
        ':input[name="field_a"]' =&gt; ['!value' =&gt; '_none']
      ],
      'required' =&gt; [
        ':input[name="field_a"]' =&gt; ['!value' =&gt; '_none'],
      ],
  ];


Don't forget add the js code in your_module.libraries.yml file

Adding JS to a Drupal module

With our above code it worked!.