title: Creating Yii FormModel objects (CFormModel) dynamically
body: <p>I'm working on an application that involves generating forms at a high level of abstraction (it's a CMS app). I want to dynamically create CFormModel objects and set the form fields on-the-fly. I think I can do this by extending CFormModel, and then dynamically creating the class properites that represent the form fields ('attributes' in the Yii lingo).</p>
  
  <p>To illustrate, instead of specifying a login form in the following class (defined in a file):</p>
  
  <pre><code>// From: http://www.yiiframework.com/doc/guide/1.1/en/form.model
  class LoginForm extends CFormModel
  {
      public $username;
      public $password;
      public $rememberMe=false;
  
      private $_identity;
  
      public function rules()
      {
          return array(
              array('username, password', 'required'),
              array('rememberMe', 'boolean'),
              array('password', 'authenticate'), // assume function authenticate defined elsewhere
          );
      }
  }
  </code></pre>
  
  <p>I want to do it as follows:</p>
  
  <pre><code>class MyFormModel extends CFormModel {
  
      protected $_rules = array();
  
      public function __construct($attributes=array(), $rules=array()) {
  
          foreach ($attributes as $i =&gt; $attr) {
              $this-&gt;{$attr} = ???; // &lt;&lt;== This is the key here
          }
  
          // pass in array of rules as described in Yii doc for CFormModel
          $this-&gt;_rules = $rules;
      }
  
      public function rules() {
          return $_rules;
      }
  }
  </code></pre>
  
  <p>And invoke it when needed as follows:</p>
  
  <pre><code>$myModelObj = new MyFormModel($attr, $rules);
  </code></pre>
  
  <p>where :</p>
  
  <pre><code>$attr = array(
              'username',
              'rememberMe',
              'password',
          );
  $rules = array(
              array('username, password', 'required'),
              array('rememberMe', 'boolean'),
              array('password', 'authenticate'), // assume function authenticate defined elsewhere
          );
  </code></pre>
  
  <p>Note in what I'm trying to accomplish, there is no "LoginClass" written in any file, it's created on-the-fly in code.</p>
  
  <p>This would allo me to create forms (in views) doing stuff like this:</p>
  
  <pre><code>// based on http://www.yiiframework.com/doc/guide/1.1/en/form.view
  &lt;?php echo $wForm-&gt;textField($myModelObj,'username'); ?&gt;
  </code></pre>
  
  <p>etc.</p>
  
  <p>I've tried this, and the $this->{$attr} line is failing with:</p>
  
  <pre><code>Property "MyFormModel.username" is not defined.
  </code></pre>
  
  <p>Actually the code for that line is just:</p>
  
  <pre><code>$this-&gt;{$attr};
  </code></pre>
  
  <p>The ??? indicates I'm not really sure what to assign to this. In the Yii doc examples, they just define the fields as public class variables. </p>
  
  <p>Should I be using magic methods? </p>
  
  <p>Is what I'm trying to do here even possible?</p>