CRegularExpressionValidator.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * CRegularExpressionValidator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CRegularExpressionValidator validates that the attribute value matches to the specified {@link pattern regular expression}.
  12. * You may invert the validation logic with help of the {@link not} property (available since 1.1.5).
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @package system.validators
  16. * @since 1.0
  17. */
  18. class CRegularExpressionValidator extends CValidator
  19. {
  20. /**
  21. * @var string the regular expression to be matched with
  22. */
  23. public $pattern;
  24. /**
  25. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  26. * meaning that if the attribute is empty, it is considered valid.
  27. */
  28. public $allowEmpty=true;
  29. /**
  30. * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
  31. * the regular expression defined via {@link pattern} should NOT match the attribute value.
  32. * @since 1.1.5
  33. **/
  34. public $not=false;
  35. /**
  36. * Validates the attribute of the object.
  37. * If there is any error, the error message is added to the object.
  38. * @param CModel $object the object being validated
  39. * @param string $attribute the attribute being validated
  40. * @throws CException if given {@link pattern} is empty
  41. */
  42. protected function validateAttribute($object,$attribute)
  43. {
  44. $value=$object->$attribute;
  45. if($this->allowEmpty && $this->isEmpty($value))
  46. return;
  47. if($this->pattern===null)
  48. throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.'));
  49. // reason of array checking explained here: https://github.com/yiisoft/yii/issues/1955
  50. if(is_array($value) ||
  51. (!$this->not && !preg_match($this->pattern,$value)) ||
  52. ($this->not && preg_match($this->pattern,$value)))
  53. {
  54. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is invalid.');
  55. $this->addError($object,$attribute,$message);
  56. }
  57. }
  58. /**
  59. * Returns the JavaScript needed for performing client-side validation.
  60. * @param CModel $object the data object being validated
  61. * @param string $attribute the name of the attribute to be validated.
  62. * @throws CException if given {@link pattern} is empty
  63. * @return string the client-side validation script.
  64. * @see CActiveForm::enableClientValidation
  65. * @since 1.1.7
  66. */
  67. public function clientValidateAttribute($object,$attribute)
  68. {
  69. if($this->pattern===null)
  70. throw new CException(Yii::t('yii','The "pattern" property must be specified with a valid regular expression.'));
  71. $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is invalid.');
  72. $message=strtr($message, array(
  73. '{attribute}'=>$object->getAttributeLabel($attribute),
  74. ));
  75. $pattern=$this->pattern;
  76. $pattern=preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
  77. $delim=substr($pattern, 0, 1);
  78. $endpos=strrpos($pattern, $delim, 1);
  79. $flag=substr($pattern, $endpos + 1);
  80. if ($delim!=='/')
  81. $pattern='/' . str_replace('/', '\\/', substr($pattern, 1, $endpos - 1)) . '/';
  82. else
  83. $pattern = substr($pattern, 0, $endpos + 1);
  84. if (!empty($flag))
  85. $pattern .= preg_replace('/[^igm]/', '', $flag);
  86. return "
  87. if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '').($this->not ? '' : '!')."value.match($pattern)) {
  88. messages.push(".CJSON::encode($message).");
  89. }
  90. ";
  91. }
  92. }