Dynamic Form Model

Hello,

I’m really new to Yii and having trouble grasping the form model concept.

I have a Chart of Accounts (COA) database table containing master_account_ids and account_names.

I am creating a form that will list each account name along with an input field for the user entry. The input field name will correspond to the master_account_id, and the input value will be a dollar amount. I’ve retrieved the COA data and passed it to the view, and I have the following snippet:


foreach($coa as $row) {

  echo '<tr><td>'.$row->display_label.'</td><td>'.$form->textfield($model,$row->master_account_id).'</td></tr>';

}

This produces an error: Property "MonthlyAccountEntriesForm.501001 is not defined."

Must I define each master_account_id (listed here as 501001) in the model? (There will be hundreds based on the selected COA.) Or do I avoid using the form helper and just type it out as:


foreach($coa as $row) {

  echo '<tr><td>'.$row->display_label.'</td><td><input type="text" name="'.$row->master_account_id.'"></td></tr>';

}

Looking for some guidance.

Thanks

Try changing this to:


foreach($coa as $row) {

  echo '<tr><td>'.$row->display_label.'</td><td>'.$form->textfield($row,'your_money_field', array('name'=>'result_'.$row->master_account_id)).'</td></tr>';

}

Replace ‘your_money_field’ with the name of the field you’re storing your dollar amount in (leave the quotes though). Then in your controller you can add this:




if(isset($_POST) && count($_POST)>0) {echo '<pre>'; print_r($_POST); echo '</pre>'; }



That should get you going in the right direction. You’ll want to loop through the results and parse out the ID for each one.

Many thanks. The documentation is good, but sometime its a bit confusing and difficult to find the right syntax. You comments really helped.

Regards,

Kevin