title: How to validate the attributes before calling the method save in CActiveRecord?
body: <p>Say I have a model project, the owner_id stands for the owner of the project. When creating a new project, user need to type the name of the owner instead of id. I  write a method to validate to make sure that the user do exist in the system and then I can get the id of the user. The rule is:</p>
  
  <pre><code> array('owner_name', 'userValidation')
  </code></pre>
  
  <p>But I find that the validation is called only when the statement below is executing</p>
  
  <pre><code> $model-&gt;save()
  </code></pre>
  
  <p>which means that if I try to get the id of an user not in the system before the saving with the statements</p>
  
  <pre><code> $model-&gt;validate();
   $user=User::model()-&gt;findByAttributes(array('username'=&gt;$model-&gt;owner_name));
   $model-&gt;owner_id=$user-&gt;id;
  </code></pre>
  
  <p>there will be an exception. So my question is why the validation doesn't work as expected when I call the validate() method? I have checked the source code of method save in Yii</p>
  
  <pre><code> public function save($runValidation=true,$attributes=null)
      {
              if(!$runValidation || $this-&gt;validate($attributes))
                      return $this-&gt;getIsNewRecord() ? $this-&gt;insert($attributes) : $this-&gt;update($attributes);
              else
                      return false;
      }
  </code></pre>
  
  <p>I think it also try to validate by call the validate().</p>