Merge pull request #10782 from yiisoft/ip-validator-message

IpValidator: use message property for default error
This commit is contained in:
Dmitry Naumenko
2016-02-09 23:39:27 +02:00
2 changed files with 33 additions and 20 deletions

View File

@@ -355,7 +355,7 @@ yii.validation = (function ($) {
return; return;
} }
if (options.negation === false && negation !== null) { if (options.negation === false && negation !== null) {
pub.addMessage(messages, options.messages.wrongIp, value); pub.addMessage(messages, options.messages.message, value);
return; return;
} }
@@ -364,14 +364,14 @@ yii.validation = (function ($) {
pub.addMessage(messages, options.messages.ipv6NotAllowed, value); pub.addMessage(messages, options.messages.ipv6NotAllowed, value);
} }
if (!(new RegExp(options.ipv6Pattern)).test(value)) { if (!(new RegExp(options.ipv6Pattern)).test(value)) {
pub.addMessage(messages, options.messages.wrongIp, value); pub.addMessage(messages, options.messages.message, value);
} }
} else { } else {
if (!options.ipv4) { if (!options.ipv4) {
pub.addMessage(messages, options.messages.ipv4NotAllowed, value); pub.addMessage(messages, options.messages.ipv4NotAllowed, value);
} }
if (!(new RegExp(options.ipv4Pattern)).test(value)) { if (!(new RegExp(options.ipv4Pattern)).test(value)) {
pub.addMessage(messages, options.messages.wrongIp, value); pub.addMessage(messages, options.messages.message, value);
} }
} }
} }

View File

@@ -19,6 +19,19 @@ use yii\web\JsExpression;
* *
* It also may change attribute's value if normalization of IPv6 expansion is enabled. * 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
* ```
*
* @author Dmitry Naumenko <d.naumenko.a@gmail.com> * @author Dmitry Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.7 * @since 2.0.7
*/ */
@@ -112,6 +125,15 @@ class IpValidator extends Validator
* @var string Regexp-pattern to validate IPv6 address * @var string Regexp-pattern to validate IPv6 address
*/ */
public $ipv6Pattern = '/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/'; public $ipv6Pattern = '/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/';
/**
* @var string 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;
/** /**
* @var string user-defined error message is used when validation fails due to the disabled IPv6 validation. * @var string user-defined error message is used when validation fails due to the disabled IPv6 validation.
* *
@@ -144,15 +166,6 @@ class IpValidator extends Validator
* @see subnet * @see subnet
*/ */
public $wrongCidr; public $wrongCidr;
/**
* @var string 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 $wrongIp;
/** /**
* @var string user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', * @var string user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only',
* but the CIDR prefix is not set. * but the CIDR prefix is not set.
@@ -211,6 +224,9 @@ class IpValidator extends Validator
throw new InvalidConfigException('IPv6 validation can not be used. PHP is compiled without IPv6'); throw new InvalidConfigException('IPv6 validation can not be used. PHP is compiled without IPv6');
} }
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} must be a valid IP address.');
}
if ($this->ipv6NotAllowed === null) { if ($this->ipv6NotAllowed === null) {
$this->ipv6NotAllowed = Yii::t('yii', '{attribute} must not be an IPv6 address.'); $this->ipv6NotAllowed = Yii::t('yii', '{attribute} must not be an IPv6 address.');
} }
@@ -220,9 +236,6 @@ class IpValidator extends Validator
if ($this->wrongCidr === null) { if ($this->wrongCidr === null) {
$this->wrongCidr = Yii::t('yii', '{attribute} contains wrong subnet mask.'); $this->wrongCidr = Yii::t('yii', '{attribute} contains wrong subnet mask.');
} }
if ($this->wrongIp === null) {
$this->wrongIp = Yii::t('yii', '{attribute} must be a valid IP address.');
}
if ($this->noSubnet === null) { if ($this->noSubnet === null) {
$this->noSubnet = Yii::t('yii', '{attribute} must be an IP address with specified subnet.'); $this->noSubnet = Yii::t('yii', '{attribute} must be an IP address with specified subnet.');
} }
@@ -321,7 +334,7 @@ class IpValidator extends Validator
private function validateSubnet($ip) private function validateSubnet($ip)
{ {
if (!is_string($ip)) { if (!is_string($ip)) {
return [$this->wrongIp, []]; return [$this->message, []];
} }
$negation = null; $negation = null;
@@ -341,7 +354,7 @@ class IpValidator extends Validator
return [$this->hasSubnet, []]; return [$this->hasSubnet, []];
} }
if ($this->negation === false && $negation !== null) { if ($this->negation === false && $negation !== null) {
return [$this->wrongIp, []]; return [$this->message, []];
} }
if ($this->getIpVersion($ip) == 6) { if ($this->getIpVersion($ip) == 6) {
@@ -358,7 +371,7 @@ class IpValidator extends Validator
return [$this->ipv6NotAllowed, []]; return [$this->ipv6NotAllowed, []];
} }
if (!$this->validateIPv6($ip)) { if (!$this->validateIPv6($ip)) {
return [$this->wrongIp, []]; return [$this->message, []];
} }
if ($this->expandIPv6) { if ($this->expandIPv6) {
@@ -378,7 +391,7 @@ class IpValidator extends Validator
return [$this->ipv4NotAllowed, []]; return [$this->ipv4NotAllowed, []];
} }
if (!$this->validateIPv4($ip)) { if (!$this->validateIPv4($ip)) {
return [$this->wrongIp, []]; return [$this->message, []];
} }
} }
@@ -577,7 +590,7 @@ class IpValidator extends Validator
$messages = [ $messages = [
'ipv6NotAllowed' => $this->ipv6NotAllowed, 'ipv6NotAllowed' => $this->ipv6NotAllowed,
'ipv4NotAllowed' => $this->ipv4NotAllowed, 'ipv4NotAllowed' => $this->ipv4NotAllowed,
'wrongIp' => $this->wrongIp, 'message' => $this->message,
'noSubnet' => $this->noSubnet, 'noSubnet' => $this->noSubnet,
'hasSubnet' => $this->hasSubnet, 'hasSubnet' => $this->hasSubnet,
]; ];