From 93ed683502c088a82a712aa738e212dc5514b386 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 7 Feb 2016 19:30:29 +0100 Subject: [PATCH 1/3] IpValidator: use message property for default error its better to use already existing `message` property for the default error message. --- framework/validators/IpValidator.php | 35 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/framework/validators/IpValidator.php b/framework/validators/IpValidator.php index ac3fc08f11..8c94d02622 100644 --- a/framework/validators/IpValidator.php +++ b/framework/validators/IpValidator.php @@ -112,6 +112,15 @@ class IpValidator extends Validator * @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]))$/'; + /** + * @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. * @@ -144,16 +153,6 @@ class IpValidator extends Validator * @see subnet */ 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', * but the CIDR prefix is not set. @@ -212,6 +211,9 @@ class IpValidator extends Validator 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) { $this->ipv6NotAllowed = Yii::t('yii', '{attribute} must not be an IPv6 address.'); } @@ -221,9 +223,6 @@ class IpValidator extends Validator if ($this->wrongCidr === null) { $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) { $this->noSubnet = Yii::t('yii', '{attribute} must be an IP address with specified subnet.'); } @@ -322,7 +321,7 @@ class IpValidator extends Validator private function validateSubnet($ip) { if (!is_string($ip)) { - return [$this->wrongIp, []]; + return [$this->message, []]; } $negation = null; @@ -342,7 +341,7 @@ class IpValidator extends Validator return [$this->hasSubnet, []]; } if ($this->negation === false && $negation !== null) { - return [$this->wrongIp, []]; + return [$this->message, []]; } if ($this->getIpVersion($ip) == 6) { @@ -359,7 +358,7 @@ class IpValidator extends Validator return [$this->ipv6NotAllowed, []]; } if (!$this->validateIPv6($ip)) { - return [$this->wrongIp, []]; + return [$this->message, []]; } if ($this->expandIPv6) { @@ -379,7 +378,7 @@ class IpValidator extends Validator return [$this->ipv4NotAllowed, []]; } if (!$this->validateIPv4($ip)) { - return [$this->wrongIp, []]; + return [$this->message, []]; } } @@ -578,7 +577,7 @@ class IpValidator extends Validator $messages = [ 'ipv6NotAllowed' => $this->ipv6NotAllowed, 'ipv4NotAllowed' => $this->ipv4NotAllowed, - 'wrongIp' => $this->wrongIp, + 'wrongIp' => $this->message, 'noSubnet' => $this->noSubnet, 'hasSubnet' => $this->hasSubnet, ]; From b804dbb98f6785154bc65467eb2b87a240539148 Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Mon, 8 Feb 2016 19:44:44 +0200 Subject: [PATCH 2/3] IpValidator JS validation: wrongIp->message renamed --- framework/assets/yii.validation.js | 6 +++--- framework/validators/IpValidator.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 562435fc1a..0f49237aa6 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -355,7 +355,7 @@ yii.validation = (function ($) { return; } if (options.negation === false && negation !== null) { - pub.addMessage(messages, options.messages.wrongIp, value); + pub.addMessage(messages, options.messages.message, value); return; } @@ -364,14 +364,14 @@ yii.validation = (function ($) { pub.addMessage(messages, options.messages.ipv6NotAllowed, value); } if (!(new RegExp(options.ipv6Pattern)).test(value)) { - pub.addMessage(messages, options.messages.wrongIp, value); + pub.addMessage(messages, options.messages.message, value); } } else { if (!options.ipv4) { pub.addMessage(messages, options.messages.ipv4NotAllowed, value); } if (!(new RegExp(options.ipv4Pattern)).test(value)) { - pub.addMessage(messages, options.messages.wrongIp, value); + pub.addMessage(messages, options.messages.message, value); } } } diff --git a/framework/validators/IpValidator.php b/framework/validators/IpValidator.php index 3ddcbf2c2c..a93c3f3f5d 100644 --- a/framework/validators/IpValidator.php +++ b/framework/validators/IpValidator.php @@ -577,7 +577,7 @@ class IpValidator extends Validator $messages = [ 'ipv6NotAllowed' => $this->ipv6NotAllowed, 'ipv4NotAllowed' => $this->ipv4NotAllowed, - 'wrongIp' => $this->message, + 'message' => $this->message, 'noSubnet' => $this->noSubnet, 'hasSubnet' => $this->hasSubnet, ]; From 63eb47fb398b46e79ad15cd5074d30c8f0ca3ed5 Mon Sep 17 00:00:00 2001 From: SilverFire - Dmitry Naumenko Date: Tue, 9 Feb 2016 09:45:32 +0200 Subject: [PATCH 3/3] IpValidator - added examples to class PHPDoc --- framework/validators/IpValidator.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/framework/validators/IpValidator.php b/framework/validators/IpValidator.php index a93c3f3f5d..044e04296d 100644 --- a/framework/validators/IpValidator.php +++ b/framework/validators/IpValidator.php @@ -19,6 +19,19 @@ use yii\web\JsExpression; * * 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 * @since 2.0.7 */