CRequiredValidator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * CRequiredValidator 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. * CRequiredValidator validates that the specified attribute does not have null or empty value.
  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. * CRequiredValidator allows for the following placeholders to be specified:
  17. * <ul>
  18. * <li>{value}: replaced with the desired value {@link requiredValue}.</li>
  19. * </ul>
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @package system.validators
  23. * @since 1.0
  24. */
  25. class CRequiredValidator extends CValidator
  26. {
  27. /**
  28. * @var mixed the desired value that the attribute must have.
  29. * If this is null, the validator will validate that the specified attribute does not have null or empty value.
  30. * If this is set as a value that is not null, the validator will validate that
  31. * the attribute has a value that is the same as this property value.
  32. * Defaults to null.
  33. */
  34. public $requiredValue;
  35. /**
  36. * @var boolean whether the comparison to {@link requiredValue} is strict.
  37. * When this is true, the attribute value and type must both match those of {@link requiredValue}.
  38. * Defaults to false, meaning only the value needs to be matched.
  39. * This property is only used when {@link requiredValue} is not null.
  40. */
  41. public $strict=false;
  42. /**
  43. * @var boolean whether the value should be trimmed with php trim() function when comparing strings.
  44. * When set to false, the attribute value is not considered empty when it contains spaces.
  45. * Defaults to true, meaning the value will be trimmed.
  46. * @since 1.1.14
  47. */
  48. public $trim=true;
  49. /**
  50. * Validates the attribute of the object.
  51. * If there is any error, the error message is added to the object.
  52. * @param CModel $object the object being validated
  53. * @param string $attribute the attribute being validated
  54. */
  55. protected function validateAttribute($object,$attribute)
  56. {
  57. $value=$object->$attribute;
  58. if($this->requiredValue!==null)
  59. {
  60. if(!$this->strict && $value!=$this->requiredValue || $this->strict && $value!==$this->requiredValue)
  61. {
  62. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be {value}.',
  63. array('{value}'=>$this->requiredValue));
  64. $this->addError($object,$attribute,$message);
  65. }
  66. }
  67. elseif($this->isEmpty($value,$this->trim))
  68. {
  69. $message=$this->message!==null?$this->message:Yii::t('yii','{attribute} cannot be blank.');
  70. $this->addError($object,$attribute,$message);
  71. }
  72. }
  73. /**
  74. * Returns the JavaScript needed for performing client-side validation.
  75. * @param CModel $object the data object being validated
  76. * @param string $attribute the name of the attribute to be validated.
  77. * @return string the client-side validation script.
  78. * @see CActiveForm::enableClientValidation
  79. * @since 1.1.7
  80. */
  81. public function clientValidateAttribute($object,$attribute)
  82. {
  83. $message=$this->message;
  84. if($this->requiredValue!==null)
  85. {
  86. if($message===null)
  87. $message=Yii::t('yii','{attribute} must be {value}.');
  88. $message=strtr($message, array(
  89. '{value}'=>$this->requiredValue,
  90. '{attribute}'=>$object->getAttributeLabel($attribute),
  91. ));
  92. return "
  93. if(value!=" . CJSON::encode($this->requiredValue) . ") {
  94. messages.push(".CJSON::encode($message).");
  95. }
  96. ";
  97. }
  98. else
  99. {
  100. if($message===null)
  101. $message=Yii::t('yii','{attribute} cannot be blank.');
  102. $message=strtr($message, array(
  103. '{attribute}'=>$object->getAttributeLabel($attribute),
  104. ));
  105. if($this->trim)
  106. $emptyCondition = "jQuery.trim(value)==''";
  107. else
  108. $emptyCondition = "value==''";
  109. return "
  110. if({$emptyCondition}) {
  111. messages.push(".CJSON::encode($message).");
  112. }
  113. ";
  114. }
  115. }
  116. }