Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\components\validators;
- use yii\validators\Validator;
- /**
- * Class PhoneValidator
- * @package app\components\validators
- */
- class PhoneValidator extends Validator
- {
- 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}$/';
- const MESSAGE = 'Ввведите корректный номер телефона.';
- /**
- * @param \yii\base\Model $model
- * @param string $attribute
- */
- public function validateAttribute($model, $attribute)
- {
- preg_match(self::REG_EXP, $model->$attribute, $matches);
- if (!$matches) {
- $model->addError($attribute, self::MESSAGE);
- }
- }
- /**
- * @param \yii\base\Model $model
- * @param string $attribute
- * @param \yii\web\View $view
- * @return null|string
- */
- public function clientValidateAttribute($model, $attribute, $view)
- {
- $reg_exp = static::REG_EXP;
- $message = static::MESSAGE;
- return
- <<<JS
- if (!$reg_exp.test(value)) {
- messages.push('$message');
- }
- JS;
- }
- }
Add Comment
Please, Sign In to add comment