Browse Source

UrlValidator and EmailValidator IDN support fixes.

tags/2.0.0-beta
resurtm 12 years ago
parent
commit
6458b8df8f
  1. 10
      yii/assets/yii.validation.js
  2. 6
      yii/validators/EmailValidator.php
  3. 6
      yii/validators/UrlValidator.php
  4. 2
      yii/widgets/ActiveField.php

10
yii/assets/yii.validation.js

@ -112,8 +112,9 @@ yii.validation = (function ($) {
var valid = true; var valid = true;
if (options.idn) { if (options.enableIDN) {
var regexp = /^(.*)@(.*)$/, matches = regexp.exec(value); var regexp = /^(.*)@(.*)$/,
matches = regexp.exec(value);
if (matches === null) { if (matches === null) {
valid = false; valid = false;
} else { } else {
@ -137,8 +138,9 @@ yii.validation = (function ($) {
var valid = true; var valid = true;
if (options.idn) { if (options.enableIDN) {
var regexp = /^([^:]+):\/\/([^\/]+)(.*)?/, matches = regexp.exec(value); var regexp = /^([^:]+):\/\/([^\/]+)(.*)?/,
matches = regexp.exec(value);
if (matches === null) { if (matches === null) {
valid = false; valid = false;
} else { } else {

6
yii/validators/EmailValidator.php

@ -51,7 +51,7 @@ class EmailValidator extends Validator
* @var boolean whether validation process should take into account IDN (internationalized domain * @var boolean whether validation process should take into account IDN (internationalized domain
* names). Defaults to false meaning that validation of emails containing IDN will always fail. * names). Defaults to false meaning that validation of emails containing IDN will always fail.
*/ */
public $idn = false; public $enableIDN = false;
/** /**
@ -94,7 +94,7 @@ class EmailValidator extends Validator
return false; return false;
} }
$domain = rtrim(substr($value, $atPosition + 1), '>'); $domain = rtrim(substr($value, $atPosition + 1), '>');
if ($this->idn) { if ($this->enableIDN) {
$value = idn_to_ascii(ltrim(substr($value, 0, $atPosition), '<')) . '@' . idn_to_ascii($domain); $value = idn_to_ascii(ltrim(substr($value, 0, $atPosition), '<')) . '@' . idn_to_ascii($domain);
} }
$valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value); $valid = preg_match($this->pattern, $value) || $this->allowName && preg_match($this->fullPattern, $value);
@ -125,7 +125,7 @@ class EmailValidator extends Validator
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,
))), ))),
'idn' => (boolean)$this->idn, 'enableIDN' => (boolean)$this->enableIDN,
); );
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1; $options['skipOnEmpty'] = 1;

6
yii/validators/UrlValidator.php

@ -42,7 +42,7 @@ class UrlValidator extends Validator
* domain names). Defaults to false meaning that validation of URLs containing IDN will always * domain names). Defaults to false meaning that validation of URLs containing IDN will always
* fail. * fail.
*/ */
public $idn = false; public $enableIDN = false;
/** /**
@ -93,7 +93,7 @@ class UrlValidator extends Validator
$pattern = $this->pattern; $pattern = $this->pattern;
} }
if ($this->idn) { if ($this->enableIDN) {
$value = preg_replace_callback('/:\/\/([^\/]+)/', function($matches) { $value = preg_replace_callback('/:\/\/([^\/]+)/', function($matches) {
return '://' . idn_to_ascii($matches[1]); return '://' . idn_to_ascii($matches[1]);
}, $value); }, $value);
@ -127,7 +127,7 @@ class UrlValidator extends Validator
'{attribute}' => $object->getAttributeLabel($attribute), '{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute, '{value}' => $object->$attribute,
))), ))),
'idn' => (boolean)$this->idn, 'enableIDN' => (boolean)$this->enableIDN,
); );
if ($this->skipOnEmpty) { if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1; $options['skipOnEmpty'] = 1;

2
yii/widgets/ActiveField.php

@ -124,7 +124,7 @@ class ActiveField extends Component
$options['class'] = implode(' ', $class); $options['class'] = implode(' ', $class);
foreach ($this->model->getActiveValidators($attribute) as $validator) { foreach ($this->model->getActiveValidators($attribute) as $validator) {
if (($validator instanceof EmailValidator || $validator instanceof UrlValidator) && $validator->idn) { if (($validator instanceof EmailValidator || $validator instanceof UrlValidator) && $validator->enableIDN) {
$this->form->view->registerAssetBundle('punycode'); $this->form->view->registerAssetBundle('punycode');
break; break;
} }

Loading…
Cancel
Save