Advertisement
palsushobhan

wcfm-submit-article-form-validation-restrictions

Jul 20th, 2023 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.24 KB | None | 0 0
  1. add_action('after_wcfm_articles_manage', function() {
  2.     ?>
  3.     <script>
  4.         jQuery(function($) {
  5.             function stripHTML(dirtyString) {
  6.                 return $("<div/>").html(dirtyString).text().trim();
  7.             }
  8.             $( document.body ).on( 'wcfm_form_validate', function(event, elem) {
  9.                 //Restrictions
  10.                 var MAX_CHARS_TITLE = 20;
  11.                 var MIN_CHARS_TITLE = 4;
  12.                 var TITLE_RESTRICTION_MSG = "Title must be between 4-20 characters";
  13.                 var MAX_CHARS_SHORT_DESCRIPTION = 200;
  14.                 var MIN_CHARS_SHORT_DESCRIPTION = 20;
  15.                 var SD_RESTRICTION_MSG = "Short description must be between 20-200 characters";
  16.                 var MAX_CHARS_DESCRIPTION = 500;
  17.                 var MIN_CHARS_DESCRIPTION = 50;
  18.                 var DESC_RESTRICTION_MSG = "Description must be between 50-500 characters";
  19.                 var MAX_NUM_TAGS = 10;
  20.                 var MIN_NUM_TAGS = 1;
  21.                 var TAGS_RESTRICTION_MSG = "Tag numbers must be between 1-10";
  22.                 var MAX_NUM_CATGS = 4;
  23.                 var MIN_NUM_CATGS = 1;
  24.                 var CATGS_RESTRICTION_MSG = "Category numbers must be between 1-4";
  25.                 var IMAGE_RESTRICTION_MSG = "Article image is mandatory";
  26.  
  27.                 var $elem = $(elem);
  28.                 //article title restrictions
  29.                 if($elem.find('input#title').length) {
  30.                     var data_val = $elem.find('input#title').val();
  31.                     if(data_val.length > MAX_CHARS_TITLE || data_val.length < MIN_CHARS_TITLE) {
  32.                         if( $wcfm_is_valid_form ) {
  33.                             $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + TITLE_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  34.                         } else {
  35.                             $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + TITLE_RESTRICTION_MSG );
  36.                         }
  37.                         $wcfm_is_valid_form = false;
  38.                         $elem.find('input#title').removeClass('wcfm_validation_success').addClass('wcfm_validation_failed');
  39.                     }
  40.                 }
  41.                 //article short description restrictions
  42.                 var excerpt = stripHTML( getWCFMEditorContent( 'excerpt' ) );
  43.                 if(excerpt.length > MAX_CHARS_SHORT_DESCRIPTION || excerpt.length < MIN_CHARS_SHORT_DESCRIPTION) {
  44.                     if( $wcfm_is_valid_form ) {
  45.                         $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + SD_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  46.                     } else {
  47.                         $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + SD_RESTRICTION_MSG );
  48.                     }
  49.                     $wcfm_is_valid_form = false;
  50.                 }
  51.  
  52.                 //article description restrictions
  53.                 var description = stripHTML( getWCFMEditorContent( 'description' ) );
  54.                 if(description.length > MAX_CHARS_DESCRIPTION || description.length < MIN_CHARS_DESCRIPTION) {
  55.                     if( $wcfm_is_valid_form ) {
  56.                         $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + DESC_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  57.                     } else {
  58.                         $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + DESC_RESTRICTION_MSG );
  59.                     }
  60.                     $wcfm_is_valid_form = false;
  61.                 }
  62.  
  63.                 //article tags restrictions
  64.                 if($elem.find('textarea#article_tags').length) {
  65.                     var tags = $elem.find('textarea#article_tags').val().trim();
  66.                     if(!tags || tags.split(',').length > MAX_NUM_TAGS || tags.length < MIN_NUM_TAGS) {
  67.                         if( $wcfm_is_valid_form ) {
  68.                             $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + TAGS_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  69.                         } else {
  70.                             $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + TAGS_RESTRICTION_MSG );
  71.                         }
  72.                         $wcfm_is_valid_form = false;
  73.                     }
  74.                 }
  75.  
  76.                 //article category restrictions
  77.                 if($elem.find('ul#article_cats_checklist').length) {
  78.                     var $catgs = $elem.find('ul#article_cats_checklist [type="checkbox"]:checked');
  79.                     if($catgs.length > MAX_NUM_CATGS || $catgs.length < MIN_NUM_CATGS) {
  80.                         if( $wcfm_is_valid_form ) {
  81.                             $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + CATGS_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  82.                         } else {
  83.                             $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + CATGS_RESTRICTION_MSG );
  84.                         }
  85.                         $wcfm_is_valid_form = false;
  86.                     }
  87.                 }
  88.  
  89.                 //article image restriction
  90.                 if($elem.find('input#featured_img').length) {
  91.                     if(!$elem.find('input#featured_img').val()) {
  92.                         if( $wcfm_is_valid_form ) {
  93.                             $('#' + $elem.attr('id') + ' .wcfm-message').html( '<span class="wcicon-status-cancelled"></span>' + IMAGE_RESTRICTION_MSG ).addClass('wcfm-error').slideDown();
  94.                         } else {
  95.                             $('#' + $elem.attr('id') + ' .wcfm-message').append( '<br /><span class="wcicon-status-cancelled"></span>' + IMAGE_RESTRICTION_MSG );
  96.                         }
  97.                         $wcfm_is_valid_form = false;
  98.                     }
  99.                 }
  100.  
  101.             } );
  102.         });
  103.     </script>
  104.     <?php
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement