Help with defaultValue of input

I'm having trouble with the default value of an input in my code. Intuitively, I would think that specifying a defaultValue for an input would mean that the input is initialized to have that default value at app load time. But I'm finding that the input is instead undefined.

For example:

input name: "testMe", type: "bool", required: true, defaultValue: true, submitOnChange: true
if (testMe) doThis()

The first time the app loads, testMe evaluates to false, even though the defaultValue was specified as true. Presumably because the testMe input has not yet been defined by user action. How do I get testMe to evaluate to true the first time the app loads?

All inputs after initial install are undefined/null.

The UI code or device has to be opened / run / display the input for the default value to be set / saved.

So if code starts running without the UI ever instantiating, that code would need to initialize the variables or otherwise treat the null values as the default value you intend.

1 Like

Right. The first time the UI code runs (in the app configuration page), testMe is evaluating to false, even though the default value is set to true. Are you saying that shouldn't be the case? When the input loads in the UI for the first time, is that what causes testMe to be set to the default value of true? That's what I would think should happen...

Actually, its value is null until the input is either set or the page submitted. What the defaultValue is doing is prior to page submission is affecting how the bool input is displayed. Once the page is submitted, the value is set to the default. This can happen from any input with submitOnChange: true.

Given that you know the default value is true, you can use testMe == null as a placeholder for it being true. Or even better, testMe != false. That can replace your test for if(testMe).

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.