title: Syntax for filed validation in Drupal 8 Form
body: <p>Hi I am trying to write a custom module in Drupal 8, the requirement of the module is to have a form with some fields in it.</p>
  
  <p>I had wrote form with some field and validation to performed for the input data.</p>
  
  <p>Form fields code looks like this</p>
  
  <pre><code>    public function buildForm(array $form, FormStateInterface $form_state) {
      $form['name'] = array(
        '#type' =&gt; 'textfield',
        '#size' =&gt; 15,
        '#title' =&gt; t('Name'),
        '#default_value' =&gt; 'user',
        '#maxlength' =&gt; 15,
        '#required' =&gt; TRUE, 
        '#weight' =&gt; 1,
      );
     }
  </code></pre>
  
  <p>Form validation code looks like this</p>
  
  <pre><code>   public function validateForm(array &amp;$form, FormStateInterface $form_state) {
  
     $name = $form_state-&gt;getValue('name');
     if (!preg_match("/^[a-zA-Z ]*$/",$name))
     {
            $form_state-&gt;setErrorByName('name', $this-&gt;t('Enter a valid name'));
     }
     }
  </code></pre>
  
  <p>According to Drupal docs in  some pages it is documented as </p>
  
  <pre><code> $form_state-&gt;setErrorByName('name', $this-&gt;t('Enter a valid name'));
  </code></pre>
  
  <p>and in some pages it is mentioned as </p>
  
  <pre><code>$form_state-&gt;setErrorByName('$name', $this-&gt;t('Enter a valid name'));
  </code></pre>
  
  <p>My doubt is what is the correct syntax for the validation?</p>
  
  <p>Observe the differnce between setErrorByName('$name') and setErrorByName('name') </p>