title: Drupal 8 Custom Form managed_file multiple upload field restrict limit number of upload files
body: <p>I have a custom form in Drupal 8. Form has managed_file field with multiple true. I am facing challenge regarding the limit of number of files upload to this field.</p>
  
  <p>I have to make this managed_file field to upload only 3 images.</p>
  
  <p>Can someone help me regarding this?
  I have tried below code.</p>
  
  <pre><code>    public function buildForm(array $form, FormStateInterface $form_state) {
  
      $form['upload_doc'] = array(
            '#type' =&gt; 'managed_file',
            '#title' =&gt; 'File Upload',
            '#upload_location' =&gt; 'public://upload_document/',
            '#required' =&gt; TRUE,
            '#multiple' =&gt; TRUE,
            '#upload_validators' =&gt; array(
              'file_validate_extensions' =&gt; array('jpg jpeg png'),
              'file_validate_size' =&gt; 2000,
            ),
          );
  
      // Add a submit button that handles the submission of the form.
          $form['actions']['submit'] = [
            '#type' =&gt; 'submit',
            '#value' =&gt; $this-&gt;t('Submit'),
          ];
  
          return $form;
  }
  
  public function validateForm(array &amp;$form, FormStateInterface $form_state) {      
        $fileObj = $form_state-&gt;getValue('upload_doc');
        if (count($fileObj) &gt; 3) {
          drupal_set_message('File limit exceed'.count($fileObj), 'error');
          return false;
        } else {
            return true;
        }
    }
  </code></pre>
  
  <p>The problem is, i am not able to validate file upload limit. It's allow to upload more than limit. Please help me
  Thank you</p>