PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/validators/CStringValidator.php

https://gitlab.com/zenfork/vektor
PHP | 183 lines | 97 code | 13 blank | 73 comment | 22 complexity | 921df1d76823b72bba1e4d1c36df7073 MD5 | raw file
  1. <?php
  2. /**
  3. * CStringValidator 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. * CStringValidator validates that the attribute value is of certain length.
  12. *
  13. * Note, this validator should only be used with string-typed attributes.
  14. *
  15. * In addition to the {@link message} property for setting a custom error message,
  16. * CStringValidator has a couple custom error messages you can set that correspond to different
  17. * validation scenarios. For defining a custom message when the string is too short,
  18. * you may use the {@link tooShort} property. Similarly with {@link tooLong}. The messages may contain
  19. * placeholders that will be replaced with the actual content. In addition to the "{attribute}"
  20. * placeholder, recognized by all validators (see {@link CValidator}), CStringValidator allows for the following
  21. * placeholders to be specified:
  22. * <ul>
  23. * <li>{min}: when using {@link tooShort}, replaced with minimum length, {@link min}, if set.</li>
  24. * <li>{max}: when using {@link tooLong}, replaced with the maximum length, {@link max}, if set.</li>
  25. * <li>{length}: when using {@link message}, replaced with the exact required length, {@link is}, if set.</li>
  26. * </ul>
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @package system.validators
  30. * @since 1.0
  31. */
  32. class CStringValidator extends CValidator
  33. {
  34. /**
  35. * @var integer maximum length. Defaults to null, meaning no maximum limit.
  36. */
  37. public $max;
  38. /**
  39. * @var integer minimum length. Defaults to null, meaning no minimum limit.
  40. */
  41. public $min;
  42. /**
  43. * @var integer exact length. Defaults to null, meaning no exact length limit.
  44. */
  45. public $is;
  46. /**
  47. * @var string user-defined error message used when the value is too short.
  48. */
  49. public $tooShort;
  50. /**
  51. * @var string user-defined error message used when the value is too long.
  52. */
  53. public $tooLong;
  54. /**
  55. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  56. * meaning that if the attribute is empty, it is considered valid.
  57. */
  58. public $allowEmpty=true;
  59. /**
  60. * @var string the encoding of the string value to be validated (e.g. 'UTF-8').
  61. * This property is used only when mbstring PHP extension is enabled.
  62. * The value of this property will be used as the 2nd parameter of the
  63. * mb_strlen() function. If this property is not set, the application charset
  64. * will be used.
  65. * If this property is set false, then strlen() will be used even if mbstring is enabled.
  66. * @since 1.1.1
  67. */
  68. public $encoding;
  69. /**
  70. * Validates the attribute of the object.
  71. * If there is any error, the error message is added to the object.
  72. * @param CModel $object the object being validated
  73. * @param string $attribute the attribute being validated
  74. */
  75. protected function validateAttribute($object,$attribute)
  76. {
  77. $value=$object->$attribute;
  78. if($this->allowEmpty && $this->isEmpty($value))
  79. return;
  80. if(is_array($value))
  81. {
  82. // https://github.com/yiisoft/yii/issues/1955
  83. $this->addError($object,$attribute,Yii::t('yii','{attribute} is invalid.'));
  84. return;
  85. }
  86. if(function_exists('mb_strlen') && $this->encoding!==false)
  87. $length=mb_strlen($value, $this->encoding ? $this->encoding : Yii::app()->charset);
  88. else
  89. $length=strlen($value);
  90. if($this->min!==null && $length<$this->min)
  91. {
  92. $message=$this->tooShort!==null?$this->tooShort:Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
  93. $this->addError($object,$attribute,$message,array('{min}'=>$this->min));
  94. }
  95. if($this->max!==null && $length>$this->max)
  96. {
  97. $message=$this->tooLong!==null?$this->tooLong:Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
  98. $this->addError($object,$attribute,$message,array('{max}'=>$this->max));
  99. }
  100. if($this->is!==null && $length!==$this->is)
  101. {
  102. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
  103. $this->addError($object,$attribute,$message,array('{length}'=>$this->is));
  104. }
  105. }
  106. /**
  107. * Returns the JavaScript needed for performing client-side validation.
  108. * @param CModel $object the data object being validated
  109. * @param string $attribute the name of the attribute to be validated.
  110. * @return string the client-side validation script.
  111. * @see CActiveForm::enableClientValidation
  112. * @since 1.1.7
  113. */
  114. public function clientValidateAttribute($object,$attribute)
  115. {
  116. $label=$object->getAttributeLabel($attribute);
  117. if(($message=$this->message)===null)
  118. $message=Yii::t('yii','{attribute} is of the wrong length (should be {length} characters).');
  119. $message=strtr($message, array(
  120. '{attribute}'=>$label,
  121. '{length}'=>$this->is,
  122. ));
  123. if(($tooShort=$this->tooShort)===null)
  124. $tooShort=Yii::t('yii','{attribute} is too short (minimum is {min} characters).');
  125. $tooShort=strtr($tooShort, array(
  126. '{attribute}'=>$label,
  127. '{min}'=>$this->min,
  128. ));
  129. if(($tooLong=$this->tooLong)===null)
  130. $tooLong=Yii::t('yii','{attribute} is too long (maximum is {max} characters).');
  131. $tooLong=strtr($tooLong, array(
  132. '{attribute}'=>$label,
  133. '{max}'=>$this->max,
  134. ));
  135. $js='';
  136. if($this->min!==null)
  137. {
  138. $js.="
  139. if(value.length<{$this->min}) {
  140. messages.push(".CJSON::encode($tooShort).");
  141. }
  142. ";
  143. }
  144. if($this->max!==null)
  145. {
  146. $js.="
  147. if(value.length>{$this->max}) {
  148. messages.push(".CJSON::encode($tooLong).");
  149. }
  150. ";
  151. }
  152. if($this->is!==null)
  153. {
  154. $js.="
  155. if(value.length!={$this->is}) {
  156. messages.push(".CJSON::encode($message).");
  157. }
  158. ";
  159. }
  160. if($this->allowEmpty)
  161. {
  162. $js="
  163. if(jQuery.trim(value)!='') {
  164. $js
  165. }
  166. ";
  167. }
  168. return $js;
  169. }
  170. }