<?php

class SubDomainValidator extends CValidator
{
    public $pattern = '/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\$*\.?$/';
    protected function validateAttribute($model, $attribute)
    {
        $val = $this->validateValue($model->domain);
        if ($val == false) {
            $message = (null !== $this->message) ? $this->message : Yii::t('yii', '{attribute} is not a valid Domain.');
            $this->addError($model, $attribute, $message);

            return;
        }
        $valid_pages = Page::model()->find('name=:name', array(':name' => $model->domain));
        if (isset($valid_pages)) {
            $message = (null !== $this->message) ? $this->message : Yii::t('yii', '{attribute} is already exists.');
            $this->addError($model, $attribute, $message);
        }
    }
    public function validateValue($value)
    {
        if (is_string($value) && strlen($value) < 2000) {
            // make sure the length is limited to avoid DOS attacks

            if (preg_match($this->pattern, $value)) {
                return $value;
            }
        }

        return false;
    }
}
