CRangeValidator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * CRangeValidator 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. * CRangeValidator validates that the attribute value is among the list (specified via {@link range}).
  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 CRangeValidator extends CValidator
  19. {
  20. /**
  21. * @var array list of valid values that the attribute value should be among
  22. */
  23. public $range;
  24. /**
  25. * @var boolean whether the comparison is strict (both type and value must be the same)
  26. */
  27. public $strict=false;
  28. /**
  29. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  30. * meaning that if the attribute is empty, it is considered valid.
  31. */
  32. public $allowEmpty=true;
  33. /**
  34. * @var boolean whether to invert the validation logic. Defaults to false. If set to true,
  35. * the attribute value should NOT be among the list of values defined via {@link range}.
  36. * @since 1.1.5
  37. **/
  38. public $not=false;
  39. /**
  40. * Validates the attribute of the object.
  41. * If there is any error, the error message is added to the object.
  42. * @param CModel $object the object being validated
  43. * @param string $attribute the attribute being validated
  44. * @throws CException if given {@link range} is not an array
  45. */
  46. protected function validateAttribute($object,$attribute)
  47. {
  48. $value=$object->$attribute;
  49. if($this->allowEmpty && $this->isEmpty($value))
  50. return;
  51. if(!is_array($this->range))
  52. throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
  53. $result = false;
  54. if($this->strict)
  55. $result=in_array($value,$this->range,true);
  56. else
  57. {
  58. foreach($this->range as $r)
  59. {
  60. $result = $r === '' || $value === '' ? $r === $value : $r == $value;
  61. if($result)
  62. break;
  63. }
  64. }
  65. if(!$this->not && !$result)
  66. {
  67. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is not in the list.');
  68. $this->addError($object,$attribute,$message);
  69. }
  70. elseif($this->not && $result)
  71. {
  72. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is in the list.');
  73. $this->addError($object,$attribute,$message);
  74. }
  75. }
  76. /**
  77. * Returns the JavaScript needed for performing client-side validation.
  78. * @param CModel $object the data object being validated
  79. * @param string $attribute the name of the attribute to be validated.
  80. * @throws CException if given {@link range} is not an array
  81. * @return string the client-side validation script.
  82. * @see CActiveForm::enableClientValidation
  83. * @since 1.1.7
  84. */
  85. public function clientValidateAttribute($object,$attribute)
  86. {
  87. if(!is_array($this->range))
  88. throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
  89. if(($message=$this->message)===null)
  90. $message=$this->not ? Yii::t('yii','{attribute} is in the list.') : Yii::t('yii','{attribute} is not in the list.');
  91. $message=strtr($message,array(
  92. '{attribute}'=>$object->getAttributeLabel($attribute),
  93. ));
  94. $range=array();
  95. foreach($this->range as $value)
  96. $range[]=(string)$value;
  97. $range=CJSON::encode($range);
  98. return "
  99. if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '').($this->not ? "jQuery.inArray(value, $range)>=0" : "jQuery.inArray(value, $range)<0").") {
  100. messages.push(".CJSON::encode($message).");
  101. }
  102. ";
  103. }
  104. }