Update add /edit form to require fields before submit with js.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 16 Feb 2018 15:26:23 +0000 (10:26 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 16 Feb 2018 15:26:23 +0000 (10:26 -0500)
Use javascript to check the required fields when submitting an event.
This helps when adding an event so users won't loose dates and times.

index.php
readme.txt
views/admin/events/edit.html

index e3a077c..da49542 100644 (file)
--- a/index.php
+++ b/index.php
@@ -4,7 +4,7 @@
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: Gaslight Media Members Database.
 
- * Version: 1.6.74
+ * Version: 1.6.75
 
  * Author: Chuck Scott
  * Author URI: http://www.gaslightmedia.com/
@@ -22,7 +22,7 @@
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
 
- * @version 1.6.74
+ * @version 1.6.75
 
  */
 
@@ -47,7 +47,7 @@ if (!defined('ABSPATH')) {
  *  so that we're sure the other add-ons see an up to date
  *  version from this plugin.
  */
-define('GLM_MEMBERS_EVENTS_PLUGIN_VERSION', '1.6.74');
+define('GLM_MEMBERS_EVENTS_PLUGIN_VERSION', '1.6.75');
 define('GLM_MEMBERS_EVENTS_PLUGIN_DB_VERSION', '0.1.4');
 
 // This is the minimum version of the GLM Members DB plugin require for this plugin.
index 2cfd84f..0d4786a 100644 (file)
@@ -25,6 +25,9 @@ e.g.
 1. Activate the plugin through the 'Plugins' menu in WordPress
 
 == Changelog ==
+= 1.6.75 =
+* Add javascript for required fields on the admin add/edit event form.
+
 = 1.6.70 =
 * Fix time output for events with no time. Doesn't display the glm-event-times
   div.
index 54859d9..13a5e66 100644 (file)
   {/if}
 
   {if $haveMember && !$lockedToMember}
-    <form action="{$thisUrl}?page=glm-members-admin-menu-member" method="post" enctype="multipart/form-data">
+    <form id="glm-event-admin-form" action="{$thisUrl}?page=glm-members-admin-menu-member" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="events">
   {else}
     {if $lockedToMember}
-    <form action="{$thisUrl}?page=glm-members-admin-menu-events-list" method="post" enctype="multipart/form-data">
+    <form id="glm-event-admin-form" action="{$thisUrl}?page=glm-members-admin-menu-events-list" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="list">
     {else}
-    <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
+    <form id="glm-event-admin-form" action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
         <input type="hidden" name="glm_action" value="list">
 
     {/if}
             var recurrencesInited = false;
             var locationsInited = false;
 
+            // function used to get everything out of the visual and text editors
+            function get_tinymce_content(){
+                if (jQuery("#wp-glm_descr-wrap").hasClass("tmce-active")){
+
+                    tinyMCE.triggerSave();
+
+                    return $('#glm_descr').val();
+                } else {
+                    // get the shortcode from the editor with [ ] as the delimiter
+                    return $("#glm_descr").val();
+                }
+            }
+            // Check for required fields
+            $('#glm-event-admin-form').submit(function(){
+                var formErrors = [];
+                {foreach $event.fieldRequired as $fieldName => $fValue}
+                    {if $fieldName != 'status' && $fieldName != 'descr'}
+                if ( {if $fValue}true{else}false{/if} ) {
+                    console.log('{$fieldName}', $('input[name="{$fieldName}"]'));
+                    console.log('{$fieldName}.val()', $('input[name="{$fieldName}"]').val());
+                    if ( !$('input[name="{$fieldName}"]').val() ) {
+                        formErrors.push('{$fieldName}');
+                    }
+                }
+                    {else if $fieldName == 'descr'}
+                if ( {if $fValue}true{else}false{/if} ) {
+                    descr = get_tinymce_content();
+                    console.log( 'descr', descr );
+                    if ( !descr ) {
+                        formErrors.push('{$fieldName}');
+                    }
+                }
+                    {/if}
+
+                {/foreach}
+                var numErrors = formErrors.length;
+                if ( numErrors > 0 ) {
+                    console.log('formErrors: ', formErrors);
+                    var msg = "The following fields are required for this Event:\n";
+                    for ( var i = 0; i < numErrors; i++ ) {
+                        msg += formErrors[i] + "\n";
+                    }
+                    alert(msg);
+                    // move to first tab
+                    $('#glm-event-descr').trigger('click');
+                    return false;
+                } else {
+                    return true;
+                }
+            });
+
             /*
              * Edit area tabs
              */