Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BooleanValidator.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 boolean 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. $valid = !$this->strict && ($value == $this->trueValue || $value == $this->falseValue)
  50. || $this->strict && ($value === $this->trueValue || $value === $this->falseValue);
  51. if (!$valid) {
  52. return [$this->message, [
  53. 'true' => $this->trueValue,
  54. 'false' => $this->falseValue,
  55. ]];
  56. } else {
  57. return null;
  58. }
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function clientValidateAttribute($model, $attribute, $view)
  64. {
  65. $options = [
  66. 'trueValue' => $this->trueValue,
  67. 'falseValue' => $this->falseValue,
  68. 'message' => Yii::$app->getI18n()->format($this->message, [
  69. 'attribute' => $model->getAttributeLabel($attribute),
  70. 'true' => $this->trueValue,
  71. 'false' => $this->falseValue,
  72. ], Yii::$app->language),
  73. ];
  74. if ($this->skipOnEmpty) {
  75. $options['skipOnEmpty'] = 1;
  76. }
  77. if ($this->strict) {
  78. $options['strict'] = 1;
  79. }
  80. ValidationAsset::register($view);
  81. return 'yii.validation.boolean(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');';
  82. }
  83. }