PHP Class yii\validators\IpValidator

It also may change attribute's value if normalization of IPv6 expansion is enabled. The following are examples of validation rules using this validator: php ['ip_address', 'ip'], // IPv4 or IPv6 address ['ip_address', 'ip', 'ipv6' => false], // IPv4 address (IPv6 is disabled) ['ip_address', 'ip', 'subnet' => true], // requires a CIDR prefix (like 10.0.0.1/24) for the IP address ['ip_address', 'ip', 'subnet' => null], // CIDR prefix is optional ['ip_address', 'ip', 'subnet' => null, 'normalize' => true], // CIDR prefix is optional and will be added when missing ['ip_address', 'ip', 'ranges' => ['192.168.0.0/24']], // only IP addresses from the specified subnet are allowed ['ip_address', 'ip', 'ranges' => ['!192.168.0.0/24', 'any']], // any IP is allowed except IP in the specified subnet ['ip_address', 'ip', 'expandIPv6' => true], // expands IPv6 address to a full notation format
Since: 2.0.7
Author: Dmitry Naumenko ([email protected])
Inheritance: extends Validator
Datei anzeigen Open project: yiisoft/yii2

Public Properties

Property Type Description
$expandIPv6 whether to expand an IPv6 address to the full notation format. Defaults to false.
$hasSubnet user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4 whether the validating value can be an IPv4 address. Defaults to true.
$ipv4NotAllowed user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv4Pattern Regexp-pattern to validate IPv4 address
$ipv6 whether the validating value can be an IPv6 address. Defaults to true.
$ipv6NotAllowed user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$ipv6Pattern Regexp-pattern to validate IPv6 address
$message user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$negation whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
$networks The network aliases, that can be used in [[ranges]]. - key - alias name - value - array of strings. String can be an IP range, IP address or another alias. String can be negated with [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
$noSubnet user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$normalize whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
$notInRange user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
$subnet whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
$wrongCidr user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated

Public Methods

Method Description
clientValidateAttribute ( $model, $attribute, $view )
getRanges ( ) : array
init ( )
setRanges ( array $ranges ) Set the IPv4 or IPv6 ranges that are allowed or forbidden.
validateAttribute ( $model, $attribute )

Protected Methods

Method Description
validateIPv4 ( string $value ) : boolean Validates IPv4 address
validateIPv6 ( string $value ) : boolean Validates IPv6 address
validateValue ( $value )

Private Methods

Method Description
expandIPv6 ( string $ip ) : string Expands an IPv6 address to it's full notation. For example 2001:db8::1 will be expanded to 2001:0db8:0000:0000:0000:0000:0000:0001
getIpParsePattern ( ) : string Used to get the Regexp pattern for initial IP address parsing
getIpVersion ( string $ip ) : integer Gets the IP version
inRange ( string $ip, integer $cidr, string $range ) : boolean Checks whether the IP is in subnet range
ip2bin ( string $ip ) : string Converts IP address to bits representation
isAllowed ( string $ip, integer $cidr ) : boolean The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list.
parseNegatedRange ( $string ) : array Parses IP address/range for the negation with [[NEGATION_CHAR]].
prepareRanges ( $ranges ) : array Prepares array to fill in [[ranges]]: - Recursively substitutes aliases, described in [[networks]] with their values - Removes duplicates
validateSubnet ( $ip ) : string | array Validates an IPv4/IPv6 address or subnet

Method Details

clientValidateAttribute() public method

public clientValidateAttribute ( $model, $attribute, $view )

getRanges() public method

public getRanges ( ) : array
return array The IPv4 or IPv6 ranges that are allowed or forbidden.

init() public method

public init ( )

setRanges() public method

The following preparation tasks are performed: - Recursively substitutes aliases (described in [[networks]]) with their values. - Removes duplicates
public setRanges ( array $ranges )
$ranges array the IPv4 or IPv6 ranges that are allowed or forbidden. When the array is empty, or the option not set, all IP addresses are allowed. Otherwise, the rules are checked sequentially until the first match is found. An IP address is forbidden, when it has not matched any of the rules. Example: ```php [ 'ranges' => [ '192.168.10.128' '!192.168.10.0/24', 'any' // allows any other IP addresses ] ] ``` In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` subnet. IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction.

validateAttribute() public method

public validateAttribute ( $model, $attribute )

validateIPv4() protected method

Validates IPv4 address
protected validateIPv4 ( string $value ) : boolean
$value string
return boolean

validateIPv6() protected method

Validates IPv6 address
protected validateIPv6 ( string $value ) : boolean
$value string
return boolean

validateValue() protected method

protected validateValue ( $value )

Property Details

$expandIPv6 public_oe property

whether to expand an IPv6 address to the full notation format. Defaults to false.
public $expandIPv6

$hasSubnet public_oe property

user-defined error message is used when validation fails due to [[subnet]] is false, but CIDR prefix is present. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: subnet
public $hasSubnet

$ipv4 public_oe property

whether the validating value can be an IPv4 address. Defaults to true.
public $ipv4

$ipv4NotAllowed public_oe property

user-defined error message is used when validation fails due to the disabled IPv4 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: ipv4
public $ipv4NotAllowed

$ipv4Pattern public_oe property

Regexp-pattern to validate IPv4 address
public $ipv4Pattern

$ipv6 public_oe property

whether the validating value can be an IPv6 address. Defaults to true.
public $ipv6

$ipv6NotAllowed public_oe property

user-defined error message is used when validation fails due to the disabled IPv6 validation. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: ipv6
public $ipv6NotAllowed

$ipv6Pattern public_oe property

Regexp-pattern to validate IPv6 address
public $ipv6Pattern

$message public_oe property

user-defined error message is used when validation fails due to the wrong IP address format. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
public $message

$negation public_oe property

whether address may have a [[NEGATION_CHAR]] character at the beginning. Defaults to false.
public $negation

$networks public_oe property

The network aliases, that can be used in [[ranges]]. - key - alias name - value - array of strings. String can be an IP range, IP address or another alias. String can be negated with [[NEGATION_CHAR]] (independent of negation option). The following aliases are defined by default: - *: any - any: 0.0.0.0/0, ::/0 - private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8 - multicast: 224.0.0.0/4, ff00::/8 - linklocal: 169.254.0.0/16, fe80::/10 - localhost: 127.0.0.0/8', ::1 - documentation: 192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32 - system: multicast, linklocal, localhost, documentation
public $networks

$noSubnet public_oe property

user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', but the CIDR prefix is not set. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: subnet
public $noSubnet

$normalize public_oe property

whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an address without it. Works only when subnet is not false. For example: - 10.0.1.5 will normalized to 10.0.1.5/32 - 2008:db0::1 will be normalized to 2008:db0::1/128 Defaults to false.
See also: subnet
public $normalize

$notInRange public_oe property

user-defined error message is used when validation fails due to IP address is not not allowed by [[ranges]] check. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: ranges
public $notInRange

$subnet public_oe property

whether the address can be an IP with CIDR subnet, like 192.168.10.0/24. The following values are possible: - false - the address must not have a subnet (default). - true - specifying a subnet is required. - null - specifying a subnet is optional.
public $subnet

$wrongCidr public_oe property

user-defined error message is used when validation fails due to the wrong CIDR. You may use the following placeholders in the message: - {attribute}: the label of the attribute being validated - {value}: the value of the attribute being validated
See also: subnet
public $wrongCidr