Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\components\validators;
  4.  
  5. use yii\validators\Validator;
  6.  
  7. /**
  8. * Class PhoneValidator
  9. * @package app\components\validators
  10. */
  11. class PhoneValidator extends Validator
  12. {
  13. const REG_EXP = '/^\+7\s\([0-9]{3}\)\s[0-9]{3}\s[0-9]{2}\s[0-9]{2}|[0-9]{10}|[0-9]{10}$/';
  14. const MESSAGE = 'Ввведите корректный номер телефона.';
  15.  
  16. /**
  17. * @param \yii\base\Model $model
  18. * @param string $attribute
  19. */
  20. public function validateAttribute($model, $attribute)
  21. {
  22. preg_match(self::REG_EXP, $model->$attribute, $matches);
  23. if (!$matches) {
  24. $model->addError($attribute, self::MESSAGE);
  25. }
  26. }
  27.  
  28. /**
  29. * @param \yii\base\Model $model
  30. * @param string $attribute
  31. * @param \yii\web\View $view
  32. * @return null|string
  33. */
  34. public function clientValidateAttribute($model, $attribute, $view)
  35. {
  36. $reg_exp = static::REG_EXP;
  37. $message = static::MESSAGE;
  38.  
  39. return
  40. <<<JS
  41. if (!$reg_exp.test(value)) {
  42. messages.push('$message');
  43. }
  44. JS;
  45. }
  46. }
Add Comment
Please, Sign In to add comment