You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.8KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\validators;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\web\JsExpression;
  11. use yii\helpers\Json;
  12. /**
  13. * RegularExpressionValidator validates that the attribute value matches the specified [[pattern]].
  14. *
  15. * If the [[not]] property is set true, the validator will ensure the attribute value do NOT match the [[pattern]].
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @since 2.0
  19. */
  20. class RegularExpressionValidator extends Validator
  21. {
  22. /**
  23. * @var string the regular expression to be matched with
  24. */
  25. public $pattern;
  26. /**
  27. * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
  28. * the regular expression defined via [[pattern]] should NOT match the attribute value.
  29. */
  30. public $not = false;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function init()
  35. {
  36. parent::init();
  37. if ($this->pattern === null) {
  38. throw new InvalidConfigException('The "pattern" property must be set.');
  39. }
  40. if ($this->message === null) {
  41. $this->message = Yii::t('yii', '{attribute} is invalid.');
  42. }
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. protected function validateValue($value)
  48. {
  49. $valid = !is_array($value) &&
  50. (!$this->not && preg_match($this->pattern, $value)
  51. || $this->not && !preg_match($this->pattern, $value));
  52. return $valid ? null : [$this->message, []];
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function clientValidateAttribute($model, $attribute, $view)
  58. {
  59. $pattern = $this->pattern;
  60. $pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
  61. $deliminator = substr($pattern, 0, 1);
  62. $pos = strrpos($pattern, $deliminator, 1);
  63. $flag = substr($pattern, $pos + 1);
  64. if ($deliminator !== '/') {
  65. $pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
  66. } else {
  67. $pattern = substr($pattern, 0, $pos + 1);
  68. }
  69. if (!empty($flag)) {
  70. $pattern .= preg_replace('/[^igm]/', '', $flag);
  71. }
  72. $options = [
  73. 'pattern' => new JsExpression($pattern),
  74. 'not' => $this->not,
  75. 'message' => Yii::$app->getI18n()->format($this->message, [
  76. 'attribute' => $model->getAttributeLabel($attribute),
  77. ], Yii::$app->language),
  78. ];
  79. if ($this->skipOnEmpty) {
  80. $options['skipOnEmpty'] = 1;
  81. }
  82. ValidationAsset::register($view);
  83. return 'yii.validation.regularExpression(value, messages, ' . Json::encode($options) . ');';
  84. }
  85. }