EnumValidator.php 880 B

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. namespace yiins\components;
  3. use CModel;
  4. use yiins\exceptions\InvalidValueException;
  5. class EnumValidator extends \CValidator
  6. {
  7. public $enumClass;
  8. public $useStrictMode = true;
  9. /**
  10. * Validates a single attribute.
  11. * This method should be overridden by child classes.
  12. * @param CModel $object the data object being validated
  13. * @param string $attribute the name of the attribute to be validated.
  14. */
  15. protected function validateAttribute($object, $attribute)
  16. {
  17. $className = $this->enumClass;
  18. try {
  19. new $className($object->$attribute, $this->useStrictMode);
  20. } catch (InvalidValueException $e) {
  21. $this->addError($object, $attribute, $e->getMessage());
  22. } catch (\UnexpectedValueException $e) {
  23. $this->addError($object, $attribute, $e->getMessage());
  24. }
  25. }
  26. }