CBooleanValidator.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * CBooleanValidator 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. * CBooleanValidator validates that the attribute value is either {@link trueValue} or {@link falseValue}.
  12. *
  13. * When using the {@link message} property to define a custom error message, the message
  14. * may contain additional placeholders that will be replaced with the actual content. In addition
  15. * to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
  16. * CBooleanValidator allows for the following placeholders to be specified:
  17. * <ul>
  18. * <li>{true}: replaced with value representing the true status {@link trueValue}.</li>
  19. * <li>{false}: replaced with value representing the false status {@link falseValue}.</li>
  20. * </ul>
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @package system.validators
  24. */
  25. class CBooleanValidator extends CValidator
  26. {
  27. /**
  28. * @var mixed the value representing true status. Defaults to '1'.
  29. */
  30. public $trueValue='1';
  31. /**
  32. * @var mixed the value representing false status. Defaults to '0'.
  33. */
  34. public $falseValue='0';
  35. /**
  36. * @var boolean whether the comparison to {@link trueValue} and {@link falseValue} is strict.
  37. * When this is true, the attribute value and type must both match those of {@link trueValue} or {@link falseValue}.
  38. * Defaults to false, meaning only the value needs to be matched.
  39. */
  40. public $strict=false;
  41. /**
  42. * @var boolean whether the attribute value can be null or empty. Defaults to true,
  43. * meaning that if the attribute is empty, it is considered valid.
  44. */
  45. public $allowEmpty=true;
  46. /**
  47. * Validates the attribute of the object.
  48. * If there is any error, the error message is added to the object.
  49. * @param CModel $object the object being validated
  50. * @param string $attribute the attribute being validated
  51. */
  52. protected function validateAttribute($object,$attribute)
  53. {
  54. $value=$object->$attribute;
  55. if($this->allowEmpty && $this->isEmpty($value))
  56. return;
  57. if(!$this->strict && $value!=$this->trueValue && $value!=$this->falseValue
  58. || $this->strict && $value!==$this->trueValue && $value!==$this->falseValue)
  59. {
  60. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be either {true} or {false}.');
  61. $this->addError($object,$attribute,$message,array(
  62. '{true}'=>$this->trueValue,
  63. '{false}'=>$this->falseValue,
  64. ));
  65. }
  66. }
  67. /**
  68. * Returns the JavaScript needed for performing client-side validation.
  69. * @param CModel $object the data object being validated
  70. * @param string $attribute the name of the attribute to be validated.
  71. * @return string the client-side validation script.
  72. * @see CActiveForm::enableClientValidation
  73. * @since 1.1.7
  74. */
  75. public function clientValidateAttribute($object,$attribute)
  76. {
  77. $message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} must be either {true} or {false}.');
  78. $message=strtr($message, array(
  79. '{attribute}'=>$object->getAttributeLabel($attribute),
  80. '{true}'=>$this->trueValue,
  81. '{false}'=>$this->falseValue,
  82. ));
  83. return "
  84. if(".($this->allowEmpty ? "jQuery.trim(value)!='' && " : '')."value!=".CJSON::encode($this->trueValue)." && value!=".CJSON::encode($this->falseValue).") {
  85. messages.push(".CJSON::encode($message).");
  86. }
  87. ";
  88. }
  89. }