Dynamic Rows During View/update
Rate Topic:

I have a model EmployeeEducation which describes the educational background of a specific employee. I want to display all retrieved information through a table view. If I use findByAttributes it returns the latest entry, but findAllByAttributes returns an array. I don't know if I'm doing it right by using foreach to separate data, also the part to submit the form:

When it comes to button for dynamically created row you have to way of doing it:
1. Using mostly PHP: you have button that will call ajax action that generates html and then injects those html code (that comes as response) at the end of the table that stores all rows.
2. Using pure javascript: you can copy existing row (or create new if in your case you can have zero rows). If copied then you'll need to change index in the attribute 'name' of all fields in the copied row.

In 1st solution you have to perform following steps:

create partial view file called eg. _form_row in the same directory you have view _education.php that contain following php code:

This is first solution of this problem and personally I wouldn't do it this way. It requires ajax call for thing than can be done using only javascript. If you want I can write you second solution if you are interested.

The first one updates the existing data but won't create new ones right?
The second one creates new data only.

By the way, is it possible to use CActiveForm instead of CHtml? I plan on having ajax on submit/client validation for the forms. But it seems impossible to pass $form from _education.php to _form_row.php, right?

... The first one updates the existing data but won't create new ones right? ...

yes you are right

checkout101, on 11 December 2012 - 08:32 AM, said:

... The second one creates new data only ...

and again yes right

checkout101, on 11 December 2012 - 08:32 AM, said:

... By the way, is it possible to use CActiveForm instead of CHtml? I plan on having ajax on submit/client validation for the forms. But it seems impossible to pass $form from _education.php to _form_row.php, right?

'CHtml::activeTextFiled' is equivalent to '$form->textFiled' and it will handle ajax validation the same way as '$form' does so you can safely use CHtml but you have to change @mrk code to make validation work.

What happens when you call $model->save() is that when this saving fails then internal error array of model is filled with error messages.
Problem in your case is because of the fact that models array in your view is retrieved directly in your view file - even if a while ago the same models were validated in your controller.
Just change your controller code into:

of course it can be much richer with labels etc, but let's omit it for clarity. Let's assume this one div with "row" class is always in view - view can generate it easily either there are some relations in DB or not. What you want to do is dynamically add more rows basing on this one existing. All you have to do is copy the whole div with class "row", then set proper indices inside name attributes of form elements. Let's code it:

You just need to assign addRowButtonHandler to onclick event for some button and then you have to adjust this piece of code for your form - name of relation_id field, number of text fields and name of attributes in model those fields are responsible for.