CRM-18560 - Validate either body text or body html
authorAnjali Ujjainia <ujjainia.2906@gmail.com>
Sun, 22 May 2016 10:32:30 +0000 (16:02 +0530)
committerAnjali Ujjainia <ujjainia.2906@gmail.com>
Sun, 22 May 2016 10:32:30 +0000 (16:02 +0530)
CRM/Mailing/Form/Component.php

index f6d0a83df21f074453078723b812857fa2f6dfba..e3e5dc301703607af08be08c0a4de6343d4ab2de 100644 (file)
@@ -76,8 +76,7 @@ class CRM_Mailing_Form_Component extends CRM_Core_Form {
       TRUE
     );
     $this->add('textarea', 'body_text', ts('Body - TEXT Format'),
-      CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_text'),
-      TRUE
+      CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_text')
     );
     $this->add('textarea', 'body_html', ts('Body - HTML Format'),
       CRM_Core_DAO::getAttribute('CRM_Mailing_DAO_Component', 'body_html')
@@ -86,6 +85,7 @@ class CRM_Mailing_Form_Component extends CRM_Core_Form {
     $this->addYesNo('is_default', ts('Default?'));
     $this->addYesNo('is_active', ts('Enabled?'));
 
+    $this->addFormRule(array('CRM_Mailing_Form_Component', 'formRule'));
     $this->addFormRule(array('CRM_Mailing_Form_Component', 'dataRule'));
 
     $this->addButtons(array(
@@ -181,7 +181,26 @@ class CRM_Mailing_Form_Component extends CRM_Core_Form {
           )) . '<ul>' . implode('', $dataErrors) . '</ul><br /><a href="' . CRM_Utils_System::docURL2('Tokens', TRUE, NULL, NULL, NULL, "wiki") . '">' . ts('More information on tokens...') . '</a>';
       }
     }
+    return empty($errors) ? TRUE : $errors;
+  }
 
+  /**
+   * Validates that either body text or body html is required.
+   * @param array $params
+   *   (ref.) an assoc array of name/value pairs.
+   *
+   * @param $files
+   * @param $options
+   *
+   * @return bool|array
+   *   mixed true or array of errors
+   */
+  public static function formRule($params, $files, $options) {
+    $errors = array();
+    if (empty($params['body_text']) && empty($params['body_html'])) {
+      $errors['body_text'] = ts("Please provide either HTML or TEXT format for the Body.");
+      $errors['body_html'] = ts("Please provide either HTML or TEXT format for the Body.");
+    }
     return empty($errors) ? TRUE : $errors;
   }