[EXTENSION] multimodelform/jqrelcopy

Hi, This is cool stuff.

I have tried to use a text-editor for one field,

EDIT*** Sorry I was a tired retard, I tried with type=>textArea instead of type=>textarea

All fine now :P


Thanks for your example.

On debugging / var_dumping through the code I have detected, that the issue with the checkboxes is not as easy I thought first :frowning:

The problem is to handle the count of the submitted values of the checkboxes.

For one cloned checkbox there can exists one (submitted by the hidden yii-attribute if not checked) or two values (hidden yii-attribute and the cloned checkbox) in the submitted attributes array.

So the count of the submitted values from cloned checkboxes never matches the count of the other attributes and the multimodelform::initItems code is wrong for this :frowning:

I need an idea first, how to handle checkboxes … in js or the initItems code.

I tested this with CFormModel and it can work with it but needs some modifications in your form and MultiModelForm.php. The CFormModel doesn’t initially have or inherit the primaryKey property and method so those need to be added to the form that is used. In addition the MultiModelForm needs to be modified to be able to read it…

public function getPrimaryKey($model)


    $result = array();

    if ($model instanceof CActiveRecord)


        $pkValue = $model->primaryKey;

        if (!empty($pkValue))


            $pkName = $model->primaryKey();

            if (empty($pkName))

                $pkName = $model->tableSchema->primaryKey;

            $result = array($pkName => $pkValue);



[b] else if($model instanceof CFormModel && method_exists($model, ‘primaryKey’) && $model->primaryKey)


        $pkValue = $model->primaryKey;

        if (!empty($pkValue))


            $pkName = $model->primaryKey();

            if (!empty($pkName))

                $result = array($pkName => $pkValue);



    else // when working with EMongoDocument

        if (method_exists($model, 'primaryKey'))


            $pkName = $model->primaryKey();

            $pkValue = $model->$pkName;

            if (empty($pkValue))

                $result = array($pkName => $pkValue);


    return $result;


Also, maybe it was mentioned somewhere but it might be worth emphasizing that the $validatedItems is an array of models used with multimodel form and the $deleteItems is an array of primary keys.

Joblo! I had ton of problems getting the cloning to work with my wysiwyg editor, multiple instances but all toolbars sent

input etc to the first instance (thats there on load). And it was impossible to write anything in the "clones".

My solution was: I took the js code rendered by my editor widget and put it in a seperate file.

I created a tiny action in my controller, that only made a renderPartial on my seperate file.

I then used ‘jsAfterClone’=>’$("#foo").load("/howto/eltre")’, in the multimodelconfiguration.

and tada, it worked like a charm.

Is it possible to just skip having the first item visible? so that nothing is shown until you press "add item" and a cloned element appears?

Yeah, that is what I was looking at.

The only way I could figure out to fix it would be to assign array indexes (just like you do for when things are already set) to everything. I think that would require several new pieces in your serverside and clientside JS.

Here is my idea:

  1. Serverside: Use the existing count variable for the existing rows but increment it by 1 and set that is the new row

  2. Serverside: Echo a new variable for JS called nextRowCount (for it to use as the index of the next row)

  3. Clientside: Have the JS grab the nextRowCount when inserting new rows and increment it after each insert

Make since? It actually might not be that hard. It just depends how much effort it will take to make the global serverside and clientside variables.

You could even use a hidden form field for the client side one. (I would, however, make you JS code fallback to its normal way of not specifying an index if the field for next row index is missing though. That way it will be reverse compatible for people who are just using the JS portion.)

Hm, If i add related records when I update a main-model record the related records are saved properly,

but If I use the same form to add related records when I create a new main-model record only the main record is saved.

I’ve tried to look for diffrences but I cant find whats causing this.

here is my controller actions and views:


Joblo, would it make sense to put the multimodelform code up on Github and then use the issue tracker to discuss this further? I’ve been looking at the checkbox issue myself for a project I’m working on. I’d be happy to help with it as well. Seems like the discussion of this issue might get lost in the other posts on this thread though … I’m happy to post your latest multimodelform extension up if that would be helpful, but thought you might want to retain ownership …

Let me know.

What if we were to shift to something more like Yii does for handling input data? I realize this could break backwards compatibility badly … (could we have a flag for the widget that defines how this gets handled?)

Yii does things like this:

<input name="Contact[35][Phone]" id="Contact_35_Phone" type="text">

where this JS handles it like this:

<input name="Contact[Phone][]" id="Contact_Phone" type="text">

Would moving to the Yii system allow you to let Yii handle the attributes better for these types of inputs too? Just thinking out loud.

I would be glad to have co-maintainers (loren, fr0d0z …) who understand the code of this extension.

I really don’t have the time to investigate and fix all issues posted here. It needs time and ideas to support more form input elements …

So it’s a good idea to put the latetest code on Github.

If you want you can do this, because I have never used Github before.

Ok, code is up on Github:


Joblo, it’s really easy. If you sign up for an account, I’ll add you as a collaborator on the code. And then we can take this issue over to Github’s Issue Tracker.

I’ve really tried to make texteditors to work for the textareas, My last solution did not work as I thought it did.

Somehow it seems to appear when adding a third element. I’ve walked thrue your code but 1200 rows and to be honest my knowledge is limited on how widgets work. Any thoughts on this? Which function and where should I try to modify?

What is the availeble variables?

Hi Everyone!

first off, thank you very much for this awesome extension. I’m very new to yii and I would like to ask for anyone’s help on how I could solve something I am not sure of with multimodelform:

I am currently working on a billing system. So basically there is a TOTAL AMOUNT DUE field in form and the value of this fied should change as you enter items for the billing items. I have the following fields for items:

quantity, amount, discount and total

As you enter quantity and amount the total box should be auto filled, and the sum of the value of the total boxes will be the value for TOTAL AMOUNT DUE field

How can i achieve this with multimodelform and yii. Thank you very much.


I was able to do the computations already thru onchange event, the only problem is the javascript is not being run if there is just one row of fields for the items… it will only start computing once I added a new row. Please help… thank you very much.


I’m facing some issues after using the MultiModelForm. In the base form, i have many MultiModelForm’s rendering.


The form values for MultiModelForm’s fields are not changing if the validation is failed. After debugging I have found the bug.

I am using MultiModelForm to render forms of different datatype for the same field. So the name of these attributes across multiple models is same due to which validation is failing.

If it’s not clear please ask again.


I don’t think this is a MMF issue, but I didn’t know where else to post this…

I have one last piece and I think my form will be complete. I want to put the multimodelform into a cJuiTabs.

When I put the widget into the tab, It doesn’t put it in the tab, but above it.

Can someone point me in the right direction?

$this->widget('zii.widgets.jui.CJuiTabs', array(

        'tabs'=>array('Standard 1'=>array($this->widget('ext.multimodelform.MultiModelForm',array(

                      'id' => 'id_perfreviewsub2', //the unique widget id

                      'formConfig' => $perfreviewsubFormConfig, //the form configuration array

                      'model' => $perfreviewsub, //instance of the form model


                      //if submitted not empty from the controller,

                      //the form will be rendered with validation errors

                      'validatedItems' => $validatedPerfReviewSub,

                      'tableView' => true,


                      //array of perfreviewsub instances loaded from db

                      'data' => $perfreviewsub->findAll(array(

                              'condition' => 'performancereviewid=:performancereviewid and standardid=:standardid',

                              'params' => array(':performancereviewid'=>$model->performancereviewid,':standardid'=>1,),

                              'order' => 'standardid, elementid ' ))



    // additional javascript options for the tabs plugin





Thank you very much.

Hello all

I can’t figure out how to solve this problem:

It’s not so easy … needs changes to the php and js-script.

But I did it, because maybe this feature is useful.

Please try the attached new release (new property $hideCopyTemplate=true by default)

and let me know, if it works for you.

Don’t forget to delete the published assets.

[EDIT] removed the attachment, see comment below

Unfortunately, this does not work for me. When I click "Add Item" nothing happens.

Would be nice, if you can submit more information.

For me with the demo-app this test-release works.

  • what does the js-console tell, any error-message?

  • are you sure, that you work with the new js-script (cache, deleted published asset)?

  • did you test with the demo-app?

Sorry, the zip-file above contains the wrong js-asset file.

I did the changes only in my published assets folder and forgot to change the extension js-file … :frowning:

Here is the new one.

[EDIT: Removed attached test code. It’s the official 3.2 release now]