Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

94 lines
2.8KB

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