All Apps and Add-ons
Highlighted

How can I control invalid user input in TextField?

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.

Highlighted

Re: How can I control invalid user input in TextField?

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, "testcasesfortextfield"

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
Highlighted

Re: How can I control invalid user input in TextField?

Communicator

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

0 Karma
Highlighted

Re: How can I control invalid user input in TextField?

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
Highlighted

Re: How can I control invalid user input in TextField?

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
Highlighted

Re: How can I control invalid user input in TextField?

Explorer

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

0 Karma