All Apps and Add-ons

How can I control invalid user input in TextField?

greg
Communicator

I have a TextField where a user can enter n-th percentile. Since the percXX function is limited to [1;99], is there any way to set the input range of the TextField?

I can't use eval to correct the value within a search:

... | eval UserInput = $TextFieldName$ | eval PercentileOK = if(UserInput < 1,1,UserInput > 99,99,UserInput) 

because there is no way then to concatenate perc and UserInput. In the mean time, perc$TextFieldName$ works, but is not protected from invalid user input.

1 Solution

sideview
SplunkTrust
SplunkTrust

Well, like with many things, you would do input validation with a "customBehavior".

CustomBehavior definitions in Sideview Utils are a pretty advanced topic and most of the people who've gotten the hang of them have done so by working with me directly. However there is a little semi-hidden docs page. Go to "Key Techniques" > Other > Tools in the app navigation. then note at the bottom the brief text and the link to this advanced docs about using the CustomBehavior module, and about the customBehavior param that is exposed on all Sideview modules.

I just put a customBehavior together to see what the code for your validation would look like. It's possible but there's a little too much "framework" exposed in the customBehavior for my taste. So I'm making some changes into the TextField module so you wont have to think about that. ie you can define three simple methods -- validate, onValidationPass and onValidationFail. These changes would be in 1.2.5, coming out in the next few days. I'll try and update this answer then, with the actual customBehavior code. If you actually want or need a customBehavior that would work with or without these changes, let me know and I can provide that too.

UPDATE:

Although I forgot to update this post, indeed the aforementioned changes went into TextField long ago. In fact the working example tailored for this exact question has been shipping for some time, as one of the examples in the hidden view in Sideview Utils, "testcases_for_textfield"

For your convenience though, here's the example laid out. Note that this example uses Splunk's messenger system, which means you'll need a Message module in the view somewhere (and if you don't like that system you probably dont need me to tell you that you can easily build your own little replacement. )

1) The XML:

<module name="TextField">
  <param name="name">field_to_validate</param>
  <param name="template">$name$="$value$"</param>
  <param name="customBehavior">customInputValidation</param>

2) The customBehavior definition, which you'd put into a JS file, typically into application.js

if (typeof(Sideview)!="undefined") {
Sideview.utils.declareCustomBehavior ("customInputValidation", function(textFieldModule) {
var messenger = Splunk.Messenger.System.getInstance();
textFieldModule.validate = function() {
var v = this.input.val();
return (Splunk.util.isInt(v) && parseInt(v)>=0 && parseInt(v)<=100);
};
textFieldModule.onValidationFail = function() {
messenger.send("info","*", "Note: percentiles must be between 0 and 100");
};
textFieldModule.onValidationPass = function() {
messenger.clear();
};
});
}

View solution in original post

0 Karma

rgustafson
Explorer

You could try using a search macro and using the "Validation Expression" option to validate your result.

0 Karma

sideview
SplunkTrust
SplunkTrust

Well, like with many things, you would do input validation with a "customBehavior".

CustomBehavior definitions in Sideview Utils are a pretty advanced topic and most of the people who've gotten the hang of them have done so by working with me directly. However there is a little semi-hidden docs page. Go to "Key Techniques" > Other > Tools in the app navigation. then note at the bottom the brief text and the link to this advanced docs about using the CustomBehavior module, and about the customBehavior param that is exposed on all Sideview modules.

I just put a customBehavior together to see what the code for your validation would look like. It's possible but there's a little too much "framework" exposed in the customBehavior for my taste. So I'm making some changes into the TextField module so you wont have to think about that. ie you can define three simple methods -- validate, onValidationPass and onValidationFail. These changes would be in 1.2.5, coming out in the next few days. I'll try and update this answer then, with the actual customBehavior code. If you actually want or need a customBehavior that would work with or without these changes, let me know and I can provide that too.

UPDATE:

Although I forgot to update this post, indeed the aforementioned changes went into TextField long ago. In fact the working example tailored for this exact question has been shipping for some time, as one of the examples in the hidden view in Sideview Utils, "testcases_for_textfield"

For your convenience though, here's the example laid out. Note that this example uses Splunk's messenger system, which means you'll need a Message module in the view somewhere (and if you don't like that system you probably dont need me to tell you that you can easily build your own little replacement. )

1) The XML:

<module name="TextField">
  <param name="name">field_to_validate</param>
  <param name="template">$name$="$value$"</param>
  <param name="customBehavior">customInputValidation</param>

2) The customBehavior definition, which you'd put into a JS file, typically into application.js

if (typeof(Sideview)!="undefined") {
Sideview.utils.declareCustomBehavior ("customInputValidation", function(textFieldModule) {
var messenger = Splunk.Messenger.System.getInstance();
textFieldModule.validate = function() {
var v = this.input.val();
return (Splunk.util.isInt(v) && parseInt(v)>=0 && parseInt(v)<=100);
};
textFieldModule.onValidationFail = function() {
messenger.send("info","*", "Note: percentiles must be between 0 and 100");
};
textFieldModule.onValidationPass = function() {
messenger.clear();
};
});
}

0 Karma

sideview
SplunkTrust
SplunkTrust

Sure can. I updated my answer to have the working example that I actually posted long ago in Utils itself. But you could easily modify it to run a regex checking for valid clientip input instead.

0 Karma

RicoSuave
Builder

will the changes you are making to the textfield module be able to monitor such things as, a user entering a hostname when he's supposed to enter an ip?

0 Karma

greg
Communicator

Thanks Nick, now I need some time to learn about CustomBehaviour. I will reply with further questions a little bit later.

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...