[Rt-commit] rt branch 5.0/allow-custom-validation-hint-on-cfs updated. rt-5.0.3-24-g5ffebdecc7

BPS Git Server git at git.bestpractical.com
Tue Feb 14 17:26:32 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/allow-custom-validation-hint-on-cfs has been updated
       via  5ffebdecc720ef228e6af8f14da09e5f36a43dd5 (commit)
       via  ddbccfcdecfe77215bce0bb929cca2fbebfdd4e2 (commit)
       via  90743ee2ff319b5687cc560523da1475e47f014d (commit)
      from  fe3fa7ec1077e5b861a98a3f7aa6151f6bc76016 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 5ffebdecc720ef228e6af8f14da09e5f36a43dd5
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Feb 14 20:15:48 2023 +0800

    Show default hint based on validation regex on cf admin pages
    
    As a default value, we don't need to store it to ValidationHint, using
    placeholder here is more appropriate.

diff --git a/share/html/Admin/CustomFields/Modify.html b/share/html/Admin/CustomFields/Modify.html
index 4693434484..4f0f1345da 100644
--- a/share/html/Admin/CustomFields/Modify.html
+++ b/share/html/Admin/CustomFields/Modify.html
@@ -186,6 +186,41 @@ jQuery( function() {
   </div>
 </div>
 
+<script type="text/javascript">
+jQuery( function() {
+    var hint_input = jQuery('#ModifyCustomField input[name=ValidationHint]');
+    var set_default_hint = function () {
+        var val = jQuery('#ModifyCustomField input[name=Pattern]').val();
+        if ( !hint_input.val() ) {
+            if ( val ) {
+                var hint;
+                var matched = val.match(/\(\?#([^)]*)\)/);
+                if ( matched ) {
+                    hint = '[' + matched[1] + ']';
+                }
+                else {
+                    hint = val;
+                }
+                hint_input.attr('placeholder', 'Input must match ' + hint);
+            }
+            else {
+                hint_input.attr('placeholder', '');
+            }
+        }
+    };
+
+    jQuery('#ModifyCustomField input[name=Pattern]').change( function() {
+        set_default_hint();
+    } );
+
+    jQuery('#ModifyCustomField input[name=ValidationHint]').change( function() {
+        if ( !jQuery(this).val() ) {
+            set_default_hint();
+        }
+    }).change();
+});
+</script>
+
 % if ( $CustomFieldObj->SupportDefaultValues ) {
 <div class="form-row">
   <div class="label col-3 edit_default_values">
diff --git a/share/static/js/util.js b/share/static/js/util.js
index 2e384a0bba..fb0e25819c 100644
--- a/share/static/js/util.js
+++ b/share/static/js/util.js
@@ -751,6 +751,11 @@ jQuery(function() {
             }
         };
 
+        // Trigger change event to update ValidationHint accordingly
+        jQuery.fn.combobox.Constructor.prototype.clearElement = function () {
+            this.$element.val('').change().focus();
+        };
+
         jQuery('.combobox').combobox({ clearIfNoMatch: false });
         jQuery('.combobox-wrapper').each( function() {
             jQuery(this).find('input[type=text]').prop('name', jQuery(this).data('name')).prop('value', jQuery(this).data('value'));

commit ddbccfcdecfe77215bce0bb929cca2fbebfdd4e2
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Feb 14 04:38:19 2023 +0800

    Customize combobox to do not clear user inputs on dropdown click

diff --git a/share/static/js/util.js b/share/static/js/util.js
index e5e93311e0..2e384a0bba 100644
--- a/share/static/js/util.js
+++ b/share/static/js/util.js
@@ -735,6 +735,22 @@ jQuery(function() {
     });
 
     if ( jQuery('.combobox').combobox ) {
+
+        // Override toggle so when user clicks the dropdown button, current value won't be cleared.
+        var orig_toggle = jQuery.fn.combobox.Constructor.prototype.toggle;
+        jQuery.fn.combobox.Constructor.prototype.toggle = function () {
+            if ( !this.disabled && !this.$container.hasClass('combobox-selected') && !this.shown && this.$element.val() ) {
+                // Show all the options
+                var matcher = this.matcher;
+                this.matcher = function () { return 1 };
+                this.lookup();
+                this.matcher = matcher;
+            }
+            else {
+                orig_toggle.apply(this);
+            }
+        };
+
         jQuery('.combobox').combobox({ clearIfNoMatch: false });
         jQuery('.combobox-wrapper').each( function() {
             jQuery(this).find('input[type=text]').prop('name', jQuery(this).data('name')).prop('value', jQuery(this).data('value'));

commit 90743ee2ff319b5687cc560523da1475e47f014d
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Feb 14 03:13:50 2023 +0800

    Extract pre-defined cf validations to @CustomFieldValuesValidations config

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index c6320ac8c5..1ef029dc3c 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -1784,6 +1784,27 @@ Set(@CustomFieldValuesCanonicalizers, qw(
     RT::CustomFieldValues::Canonicalizer::Lowercase
 ));
 
+=item C<@CustomFieldValuesValidations>
+
+Set C<@CustomFieldValuesValidations> to a list of regex strings, which will
+show up as options in "Validation" dropdown on custom field admin pages.
+
+RT will extract the optional "(?#...)" in regex to compose a friendly
+validation hint. E.g. for "(?#Mandatory).", it's
+
+    Input must match [Mandatory]
+
+You can also customize the hint via the "Validation Hint" field on custom field
+admin pages.
+
+=cut
+
+Set(@CustomFieldValuesValidations,
+    '(?#Mandatory).',
+    '(?#Digits)^[\d.]+$',
+    '(?#Year)^[12]\d{3}$',
+);
+
 =item C<%CustomFieldGroupings>
 
 This option affects the display of ticket, user, group, and asset custom fields
diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index 89276eeba7..b26e1472bd 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -1062,6 +1062,30 @@ our %META;
     ReferrerWhitelist => { Type => 'ARRAY' },
     EmailDashboardLanguageOrder  => { Type => 'ARRAY' },
     CustomFieldValuesCanonicalizers => { Type => 'ARRAY' },
+    CustomFieldValuesValidations => {
+        Type => 'ARRAY',
+        PostLoadCheck => sub {
+            my $self = shift;
+            my @values;
+            for my $value (@_) {
+                if ( defined $value ) {
+                    require RT::CustomField;
+                    my ($ret, $msg) = RT::CustomField->_IsValidRegex($value);
+                    if ($ret) {
+                        push @values, $value;
+                    }
+                    else {
+                        $RT::Logger->warning("Invalid regex '$value' in CustomFieldValuesValidations: $msg");
+                    }
+                }
+                else {
+                    $RT::Logger->warning('Empty regex in CustomFieldValuesValidations');
+                }
+
+            }
+            RT->Config->Set( CustomFieldValuesValidations => @values );
+        },
+    },
     WebPath => {
         Immutable     => 1,
         Widget        => '/Widgets/Form/String',
diff --git a/share/html/Admin/CustomFields/Modify.html b/share/html/Admin/CustomFields/Modify.html
index 0810d86159..4693434484 100644
--- a/share/html/Admin/CustomFields/Modify.html
+++ b/share/html/Admin/CustomFields/Modify.html
@@ -529,11 +529,7 @@ $EnabledChecked = '' if $CustomFieldObj->Disabled;
 my $UniqueValuesChecked = qq[checked="checked"];
 $UniqueValuesChecked = '' if !$CustomFieldObj->UniqueValues;
 
-my @CFvalidations = (
-    '(?#Mandatory).',
-    '(?#Digits)^[\d.]+$',
-    '(?#Year)^[12]\d{3}$',
-);
+my @CFvalidations = RT->Config->Get('CustomFieldValuesValidations');
 
 $m->callback(CallbackName => 'ValidationPatterns', Values => \@CFvalidations);
 

-----------------------------------------------------------------------

Summary of changes:
 etc/RT_Config.pm.in                       | 21 ++++++++++++++++
 lib/RT/Config.pm                          | 24 ++++++++++++++++++
 share/html/Admin/CustomFields/Modify.html | 41 +++++++++++++++++++++++++++----
 share/static/js/util.js                   | 21 ++++++++++++++++
 4 files changed, 102 insertions(+), 5 deletions(-)


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list