mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Fixes #8671: Extracted yii\helpers\Html::escapeJsRegularExpression() method from yii\validators\RegularExpressionValidator
This commit is contained in:
committed by
Alexander Makarov
parent
aba7effa2b
commit
b16d734c29
@ -47,6 +47,7 @@ Yii Framework 2 Change Log
|
|||||||
- Enh #8574: Added `yii\console\controllers\MessageController` support .pot file creation (pgaultier)
|
- Enh #8574: Added `yii\console\controllers\MessageController` support .pot file creation (pgaultier)
|
||||||
- Enh #8625: Added `markUnused` option to `yii\console\controllers\MessageController` (marius7383)
|
- Enh #8625: Added `markUnused` option to `yii\console\controllers\MessageController` (marius7383)
|
||||||
- Enh #8670: Added support for saving extra fields in session table for `yii\web\DbSession` (klimov-paul)
|
- Enh #8670: Added support for saving extra fields in session table for `yii\web\DbSession` (klimov-paul)
|
||||||
|
- Enh #8671: Extracted `yii\helpers\Html::escapeJsRegularExpression()` method from `yii\validators\RegularExpressionValidator` (silverfire, klimov-paul, samdark, qiangxue)
|
||||||
- Enh #8903: PostgreSQL `QueryBuilder::createIndex()` can now specify the index method to use (LAV45)
|
- Enh #8903: PostgreSQL `QueryBuilder::createIndex()` can now specify the index method to use (LAV45)
|
||||||
- Enh #9011: Allow `yii\widgets\MaskedInput` to produce an input tag of a custom type (TriAnMan)
|
- Enh #9011: Allow `yii\widgets\MaskedInput` to produce an input tag of a custom type (TriAnMan)
|
||||||
- Enh #9038: Write warning to log in case `FileCache` fails to write into file (foccy)
|
- Enh #9038: Write warning to log in case `FileCache` fails to write into file (foccy)
|
||||||
|
|||||||
@ -2095,4 +2095,29 @@ class BaseHtml
|
|||||||
$name = strtolower(static::getInputName($model, $attribute));
|
$name = strtolower(static::getInputName($model, $attribute));
|
||||||
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
|
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escapes regular expression to use in JavaScript
|
||||||
|
* @param string $regexp
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 2.0.6
|
||||||
|
*/
|
||||||
|
public static function escapeJsRegularExpression($regexp)
|
||||||
|
{
|
||||||
|
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $regexp);
|
||||||
|
$deliminator = substr($pattern, 0, 1);
|
||||||
|
$pos = strrpos($pattern, $deliminator, 1);
|
||||||
|
$flag = substr($pattern, $pos + 1);
|
||||||
|
if ($deliminator !== '/') {
|
||||||
|
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
|
||||||
|
} else {
|
||||||
|
$pattern = substr($pattern, 0, $pos + 1);
|
||||||
|
}
|
||||||
|
if (!empty($flag)) {
|
||||||
|
$pattern .= preg_replace('/[^igm]/', '', $flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new $pattern;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ namespace yii\validators;
|
|||||||
|
|
||||||
use Yii;
|
use Yii;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
|
use yii\helpers\Html;
|
||||||
use yii\web\JsExpression;
|
use yii\web\JsExpression;
|
||||||
use yii\helpers\Json;
|
use yii\helpers\Json;
|
||||||
|
|
||||||
@ -64,19 +65,7 @@ class RegularExpressionValidator extends Validator
|
|||||||
*/
|
*/
|
||||||
public function clientValidateAttribute($model, $attribute, $view)
|
public function clientValidateAttribute($model, $attribute, $view)
|
||||||
{
|
{
|
||||||
$pattern = $this->pattern;
|
$pattern = Html::escapeJsRegularExpression($this->pattern);
|
||||||
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
|
|
||||||
$deliminator = substr($pattern, 0, 1);
|
|
||||||
$pos = strrpos($pattern, $deliminator, 1);
|
|
||||||
$flag = substr($pattern, $pos + 1);
|
|
||||||
if ($deliminator !== '/') {
|
|
||||||
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
|
|
||||||
} else {
|
|
||||||
$pattern = substr($pattern, 0, $pos + 1);
|
|
||||||
}
|
|
||||||
if (!empty($flag)) {
|
|
||||||
$pattern .= preg_replace('/[^igm]/', '', $flag);
|
|
||||||
}
|
|
||||||
|
|
||||||
$options = [
|
$options = [
|
||||||
'pattern' => new JsExpression($pattern),
|
'pattern' => new JsExpression($pattern),
|
||||||
|
|||||||
Reference in New Issue
Block a user