doc/developers/README.pref_input_box.md
prefsInputFieldPrefs is a Lua function that creates input field components for ntopng preferences. It handles form rendering, validation, Redis persistence, and runtime notification for preference changes.
function prefsInputFieldPrefs(label, comment, prekey, key, default_value, _input_type, showEnabled, disableAutocomplete, allowURLs, extra)
| Parameter | Type | Description |
|---|---|---|
label | string | Display label for the input field |
comment | string | Help text shown below the label |
prekey | string | Redis key prefix for the preference |
key | string | Unique identifier for the input field |
default_value | string/number | Default value when preference is not set |
| Parameter | Type | Default | Description |
|---|---|---|---|
_input_type | string | "text" | HTML input type (text, number, password, etc.) |
showEnabled | boolean | true | Whether to display the field |
disableAutocomplete | boolean | false | Disable browser autocomplete |
allowURLs | boolean | false | Enable URL pattern validation |
extra | table | {} | Additional configuration options |
The extra parameter accepts a table with the following optional fields:
min - Minimum value (for number inputs)max - Maximum value (for number inputs)minlength - Minimum string lengthmaxlength - Maximum string lengthstep - Step value for number inputspattern - Regex pattern for validationrequired - Mark field as requiredwidth - Custom width for the inputinputBoxWidth - Specific input box width (e.g., "20em")style - Additional CSS styles (table)attributes - Additional HTML attributes (table)append - HTML to append after the inputdisabled - Disable the input fieldskip_redis - Skip Redis operations (for testing)tformat - Time format for resolution buttonsformat_spec - Format specification for time inputsprefsInputFieldPrefs(
"Server Name", -- label
"Enter the server name", -- comment
"myapp.server", -- prekey
"name", -- key
"localhost", -- default_value
"text" -- input_type
)
prefsInputFieldPrefs(
"Port Number",
"TCP port for the service (1-65535)",
"myapp.network",
"port",
"8080",
"number",
true, -- showEnabled
false, -- disableAutocomplete
false, -- allowURLs
{
min = 1,
max = 65535,
required = true,
inputBoxWidth = "8em"
}
)
prefsInputFieldPrefs(
"Database Password",
"Password for database connection",
"myapp.db",
"password",
"",
"password",
true,
true, -- disable autocomplete for passwords
false,
{
required = true,
minlength = 8,
maxlength = 64
}
)
prefsInputFieldPrefs(
"API Endpoint",
"REST API base URL",
"myapp.api",
"endpoint",
"https://api.example.com",
"url",
true,
false,
true, -- allow URLs
{
pattern = "https?://.*",
required = true,
inputBoxWidth = "25em"
}
)
local showAdvanced = ntop.getPref("myapp.show_advanced") == "1"
prefsInputFieldPrefs(
"Advanced Setting",
"This setting is only for advanced users",
"myapp.advanced",
"setting",
"default",
"text",
showAdvanced, -- only show if advanced mode is enabled
false,
false,
{
style = { ["font-family"] = "monospace" }
}
)
When a form is submitted via POST:
_POST[key] for the submitted valuentop.setPref()notifyNtopng()When rendering the form:
ntop.getPref()default_value if no stored value existsWhen allowURLs is true, the function automatically fixes common URL formatting issues:
ldaps:__ → ldaps://http:__ → http://smtp:__ → smtp://The component includes JavaScript validation that:
The Redis key is constructed as:
prekey ends with ".", then key = prekey + keyprekey + "." + keyExample:
prekey = "myapp.database"
key = "port"
-- Result: "myapp.database.port"
Use hierarchical naming for related preferences:
-- Good
"myapp.database.host"
"myapp.database.port"
"myapp.database.timeout"
-- Avoid
"db_host"
"port_db"
"timeout"
Choose sensible default values:
-- Good
default_value = "localhost" -- for hostname
default_value = "443" -- for HTTPS port
-- Avoid
default_value = "" -- for required fields
default_value = "0" -- when 0 is invalid
Always validate user input:
{
min = 1,
max = 65535,
required = true,
pattern = "^[0-9]+$" -- only digits
}
Use clear, descriptive labels:
-- Good
label = "Maximum Connection Timeout (seconds)"
comment = "How long to wait before timing out connections"
-- Avoid
label = "Timeout"
comment = "Timeout value"
The component handles several error conditions:
This component integrates with ntopng's preference system by: