Note: Setting a datatype is really a form of validation.
As such if you set a custom validation on a field, it will over-ride the datatype. or vice-versa
The error messages are handled differently.
Datatypes use a vague "Some fields are invalid, cannot save values!" message.
Where as the messages used in a custom validation appear at the bottom of the section and can be more specific. I prefer using custom validation methods
Definition
A datatype is defined as a
#!lua
cbi_element.datatype = "type"
- Validation with arguments:
#!lua
cbi_element.datatype = "type(123, 'arg2')"
- Combination of a meta type and mixed types and strings:
#!lua
cbi_element.datatype = [[ metatype(type(123, "arg2"), type, "string", 456) ]]
Meta types
and
Makes it possible to validate against one or more datatypes in the same field.
Example usage:
#!lua
cbi_element.datatype = [[ and(max(2048), uinteger) ]]
- Would allow using integers from 0 to 2048, no decimals.
or
Makes it possible to use different datatypes in the same field.
Example usage:
#!lua
cbi_element.datatype = [[ or(min(30), -1, "infinity") ]]
- Would allow using -1, a positive number except from 0 to 29 and allow the term infinity to be used.
neg
Allow using an exclamation mark in front of the value.
Example usage:
#!lua
cbi_element.datatype = "neg(ipaddr)"
list
Allow the usage of a space separated multi-value.
Types
bool
Verify if the value is either 1, yes, on, true or 0, no, off, false.
Example: on
uinteger
Verify if the value is a positive (unsigned) number and doesn't contain decimals.
Example: 2900
integer
Verify if the value is a number doesn't contain decimals.
Example: -1090
ufloat
Verify if the value is a positive number.
Example: 87.5
float
Verify if the value is a number.
Example: -192.34
ipaddr
Verify if the value is an IP address, either IPv4 or IPv6.
ip4addr
Verify if the value is an IPv4 address.
Example: 198.51.100.49
ip4prefix
Verify if the value is an IPv4 address and allow to add a mask.
Example: 203.0.113.0/24
ip6addr
Verify if the value is an IPv6 address.
Example: 2001:db8:1200::ab:45ff:fe12:3456
ip6prefix
Verify if the value is an IPv6 address and allow to add a mask.
Example: 2001:db8:1234::/48
port
Verify if the value is a port between 0 and 65535.
Example: 8080
portrange
Verify if the value contains one or more port separated by a dash.
Example: 67-69
macaddr
Verify if the value is a valid MAC address separated by colons. The
address may contain uppercase and lowercase hexadecimal characters.
Examples: 62:11:22:aa:bb:cc
hostname
Verify if the value is a valid DNS domain name or hostname. The
hostname can contain uppercase and lowercase alphanumerical characters,
underscore, dash and dots. Hostname cannot start with a dash or end with
a dot.
Example: this_IS-a.valid.host.name
host
Verify if the value is a valid hostname or IP address.
Example: openwrt.domain.local
network
Same as host but also allows UCI network names found in the Network, Interfaces page.
Example: lan
wpakey
Verify if the value is a valid WPA pre-shared key, from 8 to 63
characters or a valid WPA key containing exactly 64 hexadecimal
characters.
Example: *This is+a,valid WPA key 123%
wepkey
Verify if the value is a valid 40 bits or 104 bit WEP key or string. If the WEP key is defined as a string, it must start with s:
and contain either 5 or 13 characters. If the key is defined as
hexadecimal, it must contain exactly 10 or 26 hexadecimal characters.
Example: 123abcd890
string
Does not validate anything.
directory
Tests on the server side whether the value is a path pointing to a directory or a path pointing to a symlink of a directory.
Example: /tmp
file
Tests on the server side whether the value is a path pointing to a directory or a path pointing to a symlink of a directory.
Example: /tmp/resolv.conf
device
Tests on the server side whether the value is a path pointing to a device or a path pointing to a symlink of a device.
Example: /dev/null
uciname
Verify if the value is a valid UCI name.
Example: lan
range(min, max)
Verify if the value is a valid number between min and max.
Example: 100
min(min)
Verify if the value is a valid number above or equal to min.
Example: 152
max(max)
Verify if the value is a valid number below or equal to max.
Example: -80
rangelength(min, max)
Verify if the value fits in length between min characters and max characters.
Example: This is a test string
minlength(min)
Verify if the value has at least min characters or more.
Example: 1200
maxlength(max)
Verify if the value has at most max characters or less.
Example: 500
phonedigit
Verify if the value qualifies as a phone number containing digits from 0 to 9, star, or pound.
Example: 18005555555##123*