Answers.Body: <p>It should be something like below</p>
  
  <pre><code>function mymodule_civicrm_validateForm($formName, &amp;$fields, &amp;$files, &amp;$form, &amp;$errors) {
    if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      if ( validation condition fails )
        $errors['custom_element'] = ts('Validation message');
      }
    }
  }
  </code></pre>
  
  <p>HTH</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-21T21:24:54.997Z
Answers.Text: It didn't work.
Answers.UserDisplayName: Dilip Raj Baral
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-21T20:21:16.877Z
Answers.LastActivityDate: 2016-06-21T22:48:57.513Z
Answers.LastEditDate: 2016-06-21T22:48:57.513Z
Answers.UserDisplayName: Pradeep Nayak
Answers.Body: <p>Did you know that in your buildform hook - you can add a $form->addRule:</p>
  
  <p>two real-life examples:</p>
  
  <pre><code>$form-&gt;addRule('bank_account_number', ts('%1 must contain only digits.', array(1 =&gt; ts('Bank Account Number'))), 'numeric');
  $form-&gt;addRule('encrypted_credit_card_number', ts('%1 is a required field.', array(1 =&gt; ts('Encrypted'))), 'required');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-21T23:45:51.323Z
Answers.LastActivityDate: 2016-06-21T23:45:51.323Z
Answers.UserDisplayName: KarinG - Semper IT
Body: <p>I added a text element to a <code>New Event</code> page using <em>buildForm</em> hook.</p>
  
  <pre><code>function mymodule_civicrm_buildForm($formName, &amp;$form) {
   if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      $form-&gt;add('text', 'custom_element', ts('Custom Element Label));
      CRM_Core_Region::instance('page-body')-&gt;add(array(
       'template' =&gt; 'CustomElement.tpl',
      ));
    }
  }
  </code></pre>
  
  <p>And I added a validation code using <em>validateForm</em> hook which looks like this:</p>
  
  <pre><code>function mymodule_civicrm_validateForm($formName, &amp;$fields, &amp;$files, &amp;$form, &amp;$errors) {
    if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      if ( validation condition fails )
        $form-&gt;_errors['custom_element'] = 'Validation message';
      }
    }
  }
  </code></pre>
  
  <p>But when validation fails, let alone error, the text element is not shown at all. How do I get it to show both the element and the error on the element?</p>
  
Tags: <hooks><validation>
Title: Show validation errors on custom elements added using hooks
ThreadText: # Show validation errors on custom elements added using hooks
  
  (Asked by: username_0 on 2016-06-21)
  
  I added a text element to a `New Event` page using *buildForm* hook.
  
  ```
  function mymodule_civicrm_buildForm($formName, &$form) {
   if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      $form->add('text', 'custom_element', ts('Custom Element Label));
      CRM_Core_Region::instance('page-body')->add(array(
       'template' => 'CustomElement.tpl',
      ));
    }
  }
  
  ```
  
  And I added a validation code using *validateForm* hook which looks like this:
  
  ```
  function mymodule_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
    if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      if ( validation condition fails )
        $form->_errors['custom_element'] = 'Validation message';
      }
    }
  }
  
  ```
  
  But when validation fails, let alone error, the text element is not shown at all. How do I get it to show both the element and the error on the element?
  
  **Tags:** hooks, validation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-06-21. Score: 2
  
  It should be something like below
  
  ```
  function mymodule_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
    if ('CRM_Event_Form_ManageEvent_EventInfo' == $formName) {
      if ( validation condition fails )
        $errors['custom_element'] = ts('Validation message');
      }
    }
  }
  
  ```
  
  HTH
  
  > **Comments:**
  > **username_0**: It didn't work.  
  
  ---
  
  ### Answer by username_2 on 2016-06-21. Score: 1
  
  Did you know that in your buildform hook - you can add a $form->addRule:
  
  two real-life examples:
  
  ```
  $form->addRule('bank_account_number', ts('%1 must contain only digits.', array(1 => ts('Bank Account Number'))), 'numeric');
  $form->addRule('encrypted_credit_card_number', ts('%1 is a required field.', array(1 => ts('Encrypted'))), 'required');
  
  ```
  
  
  ---
  
  