<?php

class requiredByTemplate extends CRequiredValidator
{
    private $obj;
    private $attr;
    
    protected function validateAttribute($object,$attribute)
    {
        if (!$object->isRequiredByTemplate($attribute)) {
            return true;
        }
        parent::validateAttribute($object,$attribute);
    }
    
    public function clientValidateAttribute($object,$attribute)
    {
        
        $inputId = '#'.get_class($object)."_".$attribute;

        $message=$this->message;
        if($this->requiredValue!==null)
        {
            return parent::clientValidateAttribute($object, $attribute);
        } else {
            if($message === null) {
                $message = Yii::t('yii','{attribute} cannot be blank.');
            }
            
            $message=strtr($message, array(
                    '{attribute}' => $object->getAttributeLabel($attribute),
            ));
            
            return "
                if ($('".$inputId."').data('requiredByTemplate') === true) {
                    if($.trim(value) == '') {
                        messages.push(".CJSON::encode($message).");
                    }
                }
            ";
        }
    }
}