CRM-15932 - Fix recurring entity form validation
authorColeman Watts <coleman@civicrm.org>
Mon, 2 Mar 2015 20:37:56 +0000 (15:37 -0500)
committerColeman Watts <coleman@civicrm.org>
Mon, 2 Mar 2015 20:57:33 +0000 (15:57 -0500)
CRM/Activity/Form/Activity.php
CRM/Core/Form/RecurringEntity.php
CRM/Event/Form/ManageEvent/Repeat.php
templates/CRM/Activity/Form/Activity.tpl
templates/CRM/Core/Form/RecurringEntity.tpl
templates/CRM/Event/Form/ManageEvent/Repeat.tpl

index afba759e105b16dbec3415b5e364359fdbb06a31..14db8696de41780b640908555016d581a4332c86 100644 (file)
@@ -524,7 +524,7 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
       return CRM_Custom_Form_CustomData::setDefaultValues($this);
     }
 
-    $defaults = $this->_values;
+    $defaults = $this->_values + CRM_Core_Form_RecurringEntity::setDefaultValues();
     // if we're editing...
     if (isset($this->_activityId)) {
       if (empty($defaults['activity_date_time'])) {
@@ -536,9 +536,6 @@ class CRM_Activity_Form_Activity extends CRM_Contact_Form_Task {
           $defaults['activity_date_time_time']
           ) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
         list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($defaults['activity_date_time'], 'activityDateTime');
-        $recurringEntityDefaults = array();
-        $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues();
-        $defaults = array_merge($defaults, $recurringEntityDefaults);
       }
 
       if ($this->_context != 'standalone') {
index babbb9ec968f15d8c50d10de652a6461252f8bd9..7df05aa4872374075270fd6da2922264917df87a 100644 (file)
@@ -123,7 +123,12 @@ class CRM_Core_Form_RecurringEntity {
    * @return array
    */
   public static function setDefaultValues() {
-    $defaults = array();
+    // Defaults for new entity
+    $defaults = array(
+      'repetition_frequency_unit' => 'week',
+    );
+
+    // Default for existing entity
     if (self::$_scheduleReminderID) {
       $defaults['repetition_frequency_unit'] = self::$_scheduleReminderDetails->repetition_frequency_unit;
       $defaults['repetition_frequency_interval'] = self::$_scheduleReminderDetails->repetition_frequency_interval;
@@ -144,7 +149,6 @@ class CRM_Core_Form_RecurringEntity {
       if (self::$_scheduleReminderDetails->limit_to) {
         $defaults['repeats_by'] = 1;
       }
-      $explodeStartActionCondition = array();
       if (self::$_scheduleReminderDetails->entity_status) {
         $explodeStartActionCondition = explode(" ", self::$_scheduleReminderDetails->entity_status);
         $defaults['entity_status_1'] = $explodeStartActionCondition[0];
@@ -180,9 +184,9 @@ class CRM_Core_Form_RecurringEntity {
       'saturday',
     );
     $dayOfTheWeek = array_combine($dayOfTheWeek, CRM_Utils_Date::getAbbrWeekdayNames());
-    $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), $freqUnitsDisplay);
+    $form->add('select', 'repetition_frequency_unit', ts('Repeats every'), $freqUnitsDisplay, FALSE, array('class' => 'required'));
     $numericOptions = CRM_Core_SelectValues::getNumericOptions(1, 30);
-    $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions);
+    $form->add('select', 'repetition_frequency_interval', NULL, $numericOptions, FALSE, array('class' => 'required'));
     $form->addDateTime('repetition_start_date', ts('Repetition Start Date'), FALSE, array('formatType' => 'activityDateTime'));
     foreach ($dayOfTheWeek as $key => $val) {
       $startActionCondition[] = $form->createElement('checkbox', $key, NULL, $val);
@@ -192,9 +196,8 @@ class CRM_Core_Form_RecurringEntity {
       '1' => ts('day of the month'),
       '2' => ts('day of the week'),
     );
-    $form->addRadio('repeats_by', ts("Repeats By"), $roptionTypes, array(), NULL);
-    $getMonths = CRM_Core_SelectValues::getNumericOptions(1, 31);
-    $form->add('select', 'limit_to', '', $getMonths);
+    $form->addRadio('repeats_by', ts("Repeats by"), $roptionTypes, array('required' => TRUE), NULL);
+    $form->add('select', 'limit_to', '', CRM_Core_SelectValues::getNumericOptions(1, 31));
     $dayOfTheWeekNo = array(
       'first' => ts('First'),
       'second' => ts('Second'),
@@ -208,8 +211,8 @@ class CRM_Core_Form_RecurringEntity {
       '1' => ts('After'),
       '2' => ts('On'),
     );
-    $form->addRadio('ends', ts("Ends"), $eoptionTypes, array(), NULL);
-    $form->add('text', 'start_action_offset', '', array('size' => 3, 'maxlength' => 2));
+    $form->addRadio('ends', ts("Ends"), $eoptionTypes, array('class' => 'required'), NULL);
+    $form->add('select', 'start_action_offset', NULL, CRM_Core_SelectValues::getNumericOptions(1, 30), FALSE);
     $form->addFormRule(array('CRM_Core_Form_RecurringEntity', 'formRule'));
     $form->addDate('repeat_absolute_date', ts('On'), FALSE, array('formatType' => 'mailing'));
     $form->add('text', 'exclude_date_list', ts('Exclude Dates'), array(
index c6d4f57f9121cde0f100faf5f5cb25b77d8c97b4..2d9919092b358a154571c7d51f61313f040fe393 100644 (file)
@@ -75,7 +75,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
    * the default values are retrieved from the database
    *
    *
-   * @return void
+   * @return array
    */
   public function setDefaultValues() {
     $defaults = array();
@@ -84,8 +84,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent {
     $currentEventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'start_date', 'id');
     list($defaults['repetition_start_date'], $defaults['repetition_start_date_time']) = CRM_Utils_Date::setDateDefaults($currentEventStartDate, 'activityDateTime');
     $recurringEntityDefaults = CRM_Core_Form_RecurringEntity::setDefaultValues();
-    $defaults = array_merge($defaults, $recurringEntityDefaults);
-    return $defaults;
+    return array_merge($defaults, $recurringEntityDefaults);
   }
 
   public function buildQuickForm() {
index 096e6aa537f3741cd5d62cddfa4917aea5f9db73..77e8a3e7d45fbeac5439ab731fb660fe52559060 100644 (file)
   {if $action eq 2 OR $action eq 1}
     <tr class="crm-activity-form-block-recurring_activity">
       <td colspan="2">
-        {include file="CRM/Core/Form/RecurringEntity.tpl"}
-        {literal}
-          <script type="text/javascript">
-            CRM.$(function($) {
-              if ($('#activity_date_time').val() !== "" && $('#activity_date_time_time').val() !== "") {
-                $('#repetition_start_date, input[id^="repetition_start_date_display_"]').val($('#activity_date_time').val());
-                $('#repetition_start_date_time').val($('#activity_date_time_time').val());
-              }
-
-              $('input[id^="activity_date_time_display_"]').change(function() {
-                $('#repetition_start_date, input[id^="repetition_start_date_display_"]').val($('#activity_date_time').val());
-              });
-
-              $('#activity_date_time_time').change(function() {
-                $('#repetition_start_date_time').val($('#activity_date_time_time').val());
-              });
-
-              if ($('#start_action_offset').val() == "" && $('input[id^="repeat_absolute_date_display_"]').val() == "") {
-                $('#recurring-entity-block').addClass('collapsed');
-              }
-            });
-          </script>
-        {/literal}
-        {if $action eq 1}
-          {literal}
-            <script type="text/javascript">
-              CRM.$(function($) {
-                $('#recurring-entity-block div.crm-submit-buttons').css('display', 'none');
-              });
-            </script>
-          {/literal}
-        {/if}
+        {include file="CRM/Core/Form/RecurringEntity.tpl" recurringFormIsEmbedded=true}
       </td>
     </tr>
   {/if}
index d1580a003ef9bb326c1d5ce3aba4005402044027..8623cfaa2fbc778fbebed2d9285a2646aa05a1c7 100644 (file)
  +--------------------------------------------------------------------+\r
 *}\r
 \r
-<div class="crm-core-form-recurringentity-block crm-accordion-wrapper" id="recurring-entity-block">\r
+<div class="crm-core-form-recurringentity-block crm-accordion-wrapper{if $recurringFormIsEmbedded && !$scheduleReminderId} collapsed{/if}" id="recurring-entity-block">\r
   <div class="crm-accordion-header">\r
     {ts 1=$recurringEntityType}Repeat %1{/ts}\r
   </div>\r
   <div class="crm-accordion-body">\r
-    <div class="crm-submit-buttons">\r
-      {include file="CRM/common/formButtons.tpl" location="top"}\r
-    </div>\r
+    {if !$recurringFormIsEmbedded}\r
+      <div class="crm-submit-buttons">\r
+        {include file="CRM/common/formButtons.tpl" location="top"}\r
+      </div>\r
+    {/if}\r
     <table class="form-layout-compressed">\r
       <tr class="crm-core-form-recurringentity-block-repetition_start_date" id="tr-repetition_start_date">\r
         <td class="label">{$form.repetition_start_date.label}</td>\r
@@ -60,7 +62,7 @@
         </td>\r
       </tr>\r
       <tr class="crm-core-form-recurringentity-block-ends">\r
-        <td class="label">{$form.ends.label}&nbsp;<span class="crm-marker" title="This field is required.">*</span> {help id="id-ends-after" entityType=$recurringEntityType file="CRM/Core/Form/RecurringEntity.hlp"}</td>\r
+        <td class="label">{$form.ends.label}&nbsp;<span class="crm-marker">*</span> {help id="id-ends-after" entityType=$recurringEntityType file="CRM/Core/Form/RecurringEntity.hlp"}</td>\r
         <td>{$form.ends.1.html}&nbsp;{$form.start_action_offset.html} {ts}occurrences{/ts}</td>\r
       </tr>\r
       <tr class="crm-core-form-recurringentity-block-absolute_date">\r
         <td>{$form.exclude_date_list.html}</td>\r
       </tr>\r
     </table>\r
-    <div class="crm-submit-buttons">\r
-      {include file="CRM/common/formButtons.tpl" location="bottom"}\r
-    </div>\r
+    {if !$recurringFormIsEmbedded}\r
+      <div class="crm-submit-buttons">\r
+        {include file="CRM/common/formButtons.tpl" location="bottom"}\r
+      </div>\r
+    {/if}\r
   </div>\r
 </div>\r
 {literal}\r
   CRM.$(function($) {\r
     var $form = $('form.{/literal}{$form.formClass}{literal}');\r
 \r
-    $('#repetition_start_date_display').closest("tr").hide();\r
-    /****** On load "Repeats By" and "Repeats On" blocks should be hidden if dropdown value is not week or month****** (Edit Mode)***/\r
-    switch ($('#repetition_frequency_unit').val()) {\r
-      case 'week':\r
-        $('.crm-core-form-recurringentity-block-start_action_condition').show();\r
-        $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
-        break;\r
-      case 'month':\r
-        $('.crm-core-form-recurringentity-block-repeats_by td').show();\r
-        $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
-        break;\r
-      default:\r
-        $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
-        $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
-        break;\r
-    }\r
-\r
-    /***********On Load Set Ends Value (Edit Mode) **********/\r
-    switch ($('input:radio[name=ends]:checked').val()) {\r
-      case '1':\r
-        $('#start_action_offset').prop('disabled', false);\r
-        $('#repeat_absolute_date_display').prop('disabled', true).val('');\r
-        break;\r
-      case '2':\r
-        $('#repeat_absolute_date_display').prop('disabled', false);\r
-        $('#start_action_offset').prop('disabled', true).val('');\r
-        break;\r
-      default:\r
-        $('#start_action_offset').prop('disabled', true);\r
-        $('#repeat_absolute_date_display').prop('disabled', true);\r
-        break;\r
-    }\r
+    // Prevent html5 errors\r
+    $form.attr('novalidate', 'novalidate');\r
 \r
-    /******On Load set Repeats by section******************/\r
-    switch ($('input:radio[name=repeats_by]:checked').val()) {\r
-      case '1':\r
-        $('#limit_to').prop('disabled', false);\r
-        $('#entity_status_1, #entity_status_2').prop('disabled', true);\r
-        break;\r
-      case '2':\r
-        $('#entity_status_1, #entity_status_2').prop('disabled', false);\r
-        $('#limit_to').prop('disabled', true);\r
-        break;\r
-      default:\r
-          //Just in-case block shows up, disable it\r
-          $('#limit_to, #entity_status_1, #entity_status_2').prop('disabled', true);\r
-        break;\r
-    }\r
-\r
-    $('#repetition_frequency_unit').change(function () {\r
+    function changeFrequencyUnit() {\r
       switch ($(this).val()) {\r
-        case 'hour':\r
-          $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
-          $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
-          break;\r
-        case 'day':\r
-          $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
-          $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
-          break;\r
         case 'week':\r
           //Show "Repeats On" block when week is selected\r
-          $('.crm-core-form-recurringentity-block-start_action_condition').show();\r
-          $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
+          $('.crm-core-form-recurringentity-block-start_action_condition', $form).show();\r
+          $('.crm-core-form-recurringentity-block-repeats_by td', $form).hide();\r
           break;\r
         case 'month':\r
-          $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
           //Show "Repeats By" block when month is selected\r
-          $('.crm-core-form-recurringentity-block-repeats_by td').show();\r
-          break;\r
-        case 'year':\r
-          $('.crm-core-form-recurringentity-block-start_action_condition').hide();\r
-          $('.crm-core-form-recurringentity-block-repeats_by td').hide();\r
-          break;\r
-      }\r
-    });\r
-\r
-    // For "Ends" block\r
-    $('input:radio[name=ends]').click(function() {\r
-      switch ($(this).val()) {\r
-        case '1':\r
-          $('#start_action_offset').prop('disabled', false);\r
-          $('#repeat_absolute_date_display').val('');\r
-          break;\r
-        case '2':\r
-          $('#repeat_absolute_date_display').prop('disabled', false);\r
-          $('#start_action_offset').val('');\r
+          $('.crm-core-form-recurringentity-block-start_action_condition', $form).hide();\r
+          $('.crm-core-form-recurringentity-block-repeats_by td', $form).show();\r
           break;\r
         default:\r
-          $('#repeat_absolute_date_display').prop('disabled', true);\r
-          break;\r
+          $('.crm-core-form-recurringentity-block-start_action_condition', $form).hide();\r
+          $('.crm-core-form-recurringentity-block-repeats_by td', $form).hide();\r
       }\r
-    });\r
+    }\r
+    $('#repetition_frequency_unit', $form).each(changeFrequencyUnit).change(changeFrequencyUnit);\r
 \r
-    //For "Repeats By" block\r
-    $('input:radio[name=repeats_by]').click(function() {\r
-      $('#limit_to').prop('disabled', $(this).val() != 1);\r
-      $('#entity_status_1, #entity_status_2').prop('disabled', $(this).val() != 2);\r
-    });\r
+    function disableUnselected() {\r
+      $('input:radio[name=ends], input[name=repeats_by]', $form).not(':checked').siblings(':input').prop('disabled', true);\r
+    }\r
+    disableUnselected();\r
 \r
-    $form.submit(function() {\r
-      //Check form for values submitted\r
-      if ($('input[name=ends]:checked').val() == 1) {\r
-        if ($('#start_action_offset').val() == "") {\r
-          $('#start_action_offset').crmError();\r
-          return false;\r
-        }\r
-      } else if ($('input[name=ends]:checked').val() == 2) {\r
-        if ($('#repeat_absolute_date_display').val() == "") {\r
-          $('#repeat_absolute_date_display').crmError();\r
-          return false;\r
-        }\r
-      }\r
+    $('input:radio[name=ends], input[name=repeats_by]', $form).click(function() {\r
+      $(this).siblings(':input').prop('disabled', false).filter(':visible').addClass('required').focus();\r
+      disableUnselected();\r
     });\r
 \r
+    function validate() {\r
+      var valid = $(':input', '#recurring-entity-block').valid(),\r
+        modified = CRM.utils.initialValueChanged('#recurring-entity-block');\r
+      $('#allowRepeatConfigToSubmit', $form).val(valid && modified ? '1' : '0');\r
+      return valid;\r
+    }\r
+\r
     function getDisplayDate(date) {\r
       return $.datepicker.formatDate(CRM.config.dateInputFormat, $.datepicker.parseDate('yy-mm-dd', date));\r
     }\r
 \r
     // Combine select2 and datepicker into a multi-select-date widget\r
-    $('#exclude_date_list').crmSelect2({\r
+    $('#exclude_date_list', $form).crmSelect2({\r
       multiple: true,\r
       data: [],\r
       initSelection: function(element, callback) {\r
         callback(values);\r
       }\r
     })\r
-      // Prevent select2 from opening and show a datepicker instead\r
       .on('select2-opening', function(e) {\r
         var $el = $(this);\r
+        // Prevent select2 from opening and show a datepicker instead\r
         e.preventDefault();\r
         $('.select2-search-field input', $el.select2('container'))\r
-          .datepicker({dateFormat: CRM.config.dateInputFormat})\r
+          .datepicker()\r
           .datepicker('show')\r
           .off('.crmDate')\r
           .on('change.crmDate', function() {\r
     //If there are changes in repeat configuration, enable save button\r
     //Dialog for preview repeat Configuration dates\r
     function previewDialog() {\r
-      $('#allowRepeatConfigToSubmit').val(CRM.utils.initialValueChanged('.crm-core-form-recurringentity-block') ? '1' : '0');\r
       var payload = $form.serialize() + '&entity_table={/literal}{$entityTable}{literal}&entity_id={/literal}{$currentEntityId}{literal}',\r
         settings = CRM.utils.adjustDialogDefaults({\r
+          width: '50%',\r
           url: CRM.url("civicrm/recurringentity/preview", payload)\r
         });\r
       CRM.confirm(settings)\r
     }\r
 \r
     $('#_qf_Repeat_submit-top, #_qf_Repeat_submit-bottom').click(function (e) {\r
-      previewDialog();\r
+      if (validate()) {\r
+        previewDialog();\r
+      }\r
       e.preventDefault();\r
     });\r
 \r
-    $('#_qf_Activity_upload-top, #_qf_Activity_upload-bottom').click( function (e) {\r
-      if (CRM.utils.initialValueChanged('.crm-core-form-recurringentity-block')) {\r
+    $('#_qf_Activity_upload-top, #_qf_Activity_upload-bottom').click(function (e) {\r
+      if (CRM.utils.initialValueChanged('#recurring-entity-block')) {\r
         e.preventDefault();\r
-        previewDialog();\r
+        if (validate()) {\r
+          previewDialog();\r
+        }\r
       }\r
     });\r
 \r
index 8cd3c500a925a5618d124c06da21a73c4db14ff5..e5187ca670a0b586cb5034150a26b1d3e1085f9e 100644 (file)
@@ -24,7 +24,7 @@
  +--------------------------------------------------------------------+
 *}
 <div class="crm-block crm-form-block crm-event-manage-repeat-form-block">
-{include file="CRM/Core/Form/RecurringEntity.tpl"}
+{include file="CRM/Core/Form/RecurringEntity.tpl" recurringFormIsEmbedded=false}
 {if $rows}
 <div class="crm-block crm-manage-events crm-accordion-wrapper">
   <div class="crm-accordion-header">{ts}Connected Repeating Events{/ts}</div>