BooleanValidator.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  10. * BooleanValidator checks if the attribute value is a boolean value.
  11. *
  12. * Possible boolean values can be configured via the [[trueValue]] and [[falseValue]] properties.
  13. * And the comparison can be either [[strict]] or not.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class BooleanValidator extends Validator
  19. {
  20. /**
  21. * @var mixed the value representing true status. Defaults to '1'.
  22. */
  23. public $trueValue = '1';
  24. /**
  25. * @var mixed the value representing false status. Defaults to '0'.
  26. */
  27. public $falseValue = '0';
  28. /**
  29. * @var bool whether the comparison to [[trueValue]] and [[falseValue]] is strict.
  30. * When this is true, the attribute value and type must both match those of [[trueValue]] or [[falseValue]].
  31. * Defaults to false, meaning only the value needs to be matched.
  32. */
  33. public $strict = false;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function init()
  38. {
  39. parent::init();
  40. if ($this->message === null) {
  41. $this->message = Yii::t('yii', '{attribute} must be either "{true}" or "{false}".');
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function validateValue($value)
  48. {
  49. if ($this->strict) {
  50. $valid = $value === $this->trueValue || $value === $this->falseValue;
  51. } else {
  52. $valid = $value == $this->trueValue || $value == $this->falseValue;
  53. }
  54. if (!$valid) {
  55. return [$this->message, [
  56. 'true' => $this->trueValue === true ? 'true' : $this->trueValue,
  57. 'false' => $this->falseValue === false ? 'false' : $this->falseValue,
  58. ]];
  59. }
  60. return null;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function clientValidateAttribute($model, $attribute, $view)
  66. {
  67. ValidationAsset::register($view);
  68. $options = $this->getClientOptions($model, $attribute);
  69. return 'yii.validation.boolean(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getClientOptions($model, $attribute)
  75. {
  76. $options = [
  77. 'trueValue' => $this->trueValue,
  78. 'falseValue' => $this->falseValue,
  79. 'message' => $this->formatMessage($this->message, [
  80. 'attribute' => $model->getAttributeLabel($attribute),
  81. 'true' => $this->trueValue === true ? 'true' : $this->trueValue,
  82. 'false' => $this->falseValue === false ? 'false' : $this->falseValue,
  83. ]),
  84. ];
  85. if ($this->skipOnEmpty) {
  86. $options['skipOnEmpty'] = 1;
  87. }
  88. if ($this->strict) {
  89. $options['strict'] = 1;
  90. }
  91. return $options;
  92. }
  93. }