How to access date value from a datelist form element for validation? I have a simple form element using datelist as the type. I need to validate this to make sure the date is in the future. But I cannot work out how to access the date itself in the validation method. 

Form:

public function buildForm(array $form, FormStateInterface $form_state) {

  $form['date_field'] = array(
    '#title' =&gt; $this-&gt;t('Expiry'),
    '#type' =&gt; 'datelist',
    '#date_part_order' =&gt; array('month', 'year'),
    '#date_year_range' =&gt; '2017:2027',
    '#date_increment' =&gt; 1
  );

  return $form;
}


Validation:

public function validateForm(array &amp;$form, FormStateInterface $form_state) {

  $date_input = $form_state-&gt;getValue('date_field');

  if(strtotime(date($date_input)) &lt; strtotime(date('now'))) {  
    $form_state-&gt;setErrorByName('date_field', $this-&gt;t('The date must be in the future.'));
  }
} 


From the above, $date_input currently returns an object (see below). Essentially, I want to access $date_input->dateTimeObject->date; but doing exactly that will return a fatal error.


  Fatal error: Cannot access protected property
  Drupal\Core\Datetime\DrupalDateTime::$dateTimeObject


$date_input object:

stdClass Object
(
    [__CLASS__] =&gt; Drupal\Core\Datetime\DrupalDateTime
    [formatTranslationCache] =&gt; 
    [dateParts] =&gt; Array
        (
            [0] =&gt; year
            [1] =&gt; month
            [2] =&gt; day
            [3] =&gt; hour
            [4] =&gt; minute
            [5] =&gt; second
        )

    [inputTimeRaw] =&gt; 
    [inputTimeAdjusted] =&gt; 
    [inputTimeZoneRaw] =&gt; 
    [inputTimeZoneAdjusted] =&gt; 
    [inputFormatRaw] =&gt; 
    [inputFormatAdjusted] =&gt; 
    [langcode] =&gt; en
    [errors] =&gt; Array
        (
        )

    [dateTimeObject] =&gt; stdClass Object
        (
            [__CLASS__] =&gt; DateTime
            [date] =&gt; 2027-12-01T00:00:00+11:00
            [timezone] =&gt; Australia/Sydney
        )

    [stringTranslation] =&gt; 
)


I could do something like:

$form_state-&gt;getValue('date_field')-&gt;format('d m Y');


But I suspect there is a better way?
Yes, to compare the date it's better to use the unix timestamp. You can get it by using the method getTimeStamp() and compare it to the current date:

      $value = $form_state-&gt;getValue('date_field');
      if ($value-&gt;getTimeStamp() &gt; REQUEST_TIME) {
        drupal_set_message('Time is in the future');
      }