[EXTENSION] multimodelform/jqrelcopy

Everything works, thanks

Hi,

i wanted to use jquery-tipsy to add some tooltips to the multimodel fields in my form, but it seems not work.

Does anyone try this?

First off, thanks for the extension and example it comes with. I’m special though so I need help:

How do I add values so that every member that I’m creating has it? A date that I set in my php code rather than via UI would be an example. I thought the $masterValues array would be the place but it doesn’t seem to work.

I also have the problem that when I try to create my form, it adds an extra instance of the member boxes and complains about validation. Any ideas what may cause that kind of behaviour, the additional box?

Is $masterValues the right place to but my values that members share? Like so:


$masterValues = array ('session_id'=>$model->id,

                                      'user_id'=>$model->user_id,

                                      'date'=>$model->date,

                                      );

If that is the case then I know I’ve messed up something else. Any suggestions?

Thanks

edit:

When I don’t enter anything into the member form, it doesn’t attempt to validate it and saves the group part. When I put in values that should validate, it doesn’t accept them and creates and extra member box to fill in. I’ve tried to restart from scratch and somehow I’ve ended up with the same nonsense.

Yes, the $masterValues should be the right place for adding values to all detail records.

if you take a look at the ‘function save’ , the attributes will be assigned before saving the detail model:





 public static function save($model, &$validatedItems, &$deleteItems = array(), $masterValues = array(), $formData = null)

    {

        ...

          if (!empty($masterValues))

                   $item->setAttributes($masterValues, false);

        ...

    }



‘$item’ is the detail model and ‘setAttributes’ with $safeOnly=false is used;

So you have to check the attributes of the detail model, if this doesn’t work.

Try to debug through the code or do var_dump… etc. here.

Try this test release if you don’t want to show the additional empty member box.

Thanks for the prompt response.

The behaviour with the box makes more sense to me now (having one as default but no additions or subtractions on submit would imho be best but I have other problems ;) ).

The $masterValues still don’t work. They live in the controller and I don’t need to handle them anywhere else, right? I’m very new to this so I don’t want to start blaming other people on things not working.

I tried removing the “deleteMembers” from MultiModelForm::save, since it seems redundant in the never version and might mess with the parameters but that didn’t help either.

So now I’m lost because I’m not even sure which parts are my fault. Do people not usually use the masterValues?

I’m doing my best to find out what’s wrong:

The following line is what fails:


 if( //validate detail before saving the master

                MultiModelForm::validate($member,$validatedMembers,$deleteItems)

//                && $model->save()

            )

It doesn’t validate, because the fields that I want want to set via $masterValues are required. So what I did was take the $mastValue assignment out of the if clause and put it before the check occurs. Much to my surprise, this changes nothing.

This is my attempt:


$masterValues = array('session_id'=>$model->id,'date'=>1,'user_id'=>1);

I’m beyond lost. I don’t even know where to look for what is going wrong. I don’t know if it’s the extension or my screwed up code. I might try creating a new project just to confirm the extension does what I think it does…

Take a look at the demo app -> GroupController::actionCreate();

There the $masterValues are used to save the the foreignkey ‘groupid’ from the master model.

This must work, otherwise the whole demo-app will be useless.

Try to debug to check why the masterValues don’t work in your application.

I’ve figured it out, I’m not sure it’s a good way to go though:

I kept getting the validation message when I tried to submit and that validation happens before the $masterValues get assigned. In order to get it to work, I’ve had to take the relevant fields out of the rules array where they were required.

If I understand the example code correctly, it would fail if groupid were a required field in the relevant model?!?

This is how I understand it:

=> validating the $member


MultiModelForm::validate($member,$validatedMembers,$deleteItems)

only after that $masterValues gets assignedL


$masterValues = array('session_id'=>1,'user_id'=>1,'date'=>1);

Hence when $member gets validated, it fails, if any of the fields that $masterValues fills, are required. Moving the $masterValues before the first validation doesn’t work, I assume that’s down to how the extension works.

Does that make sense?

[size=“1”]I’m aware that in this case I’m setting the values so no need to validate them. However, there is another use of the same model where the input could come from the user. I guess I’m surprised at how this works.[/size]

A possible workaround: add a specific ‘scenario’ when creating/validating the new detail models




//----------- NEW (on validation error) -----------

public function initItems(&$validatedItems, &$deleteItems, $masterValues = array(), $formData = null)

    {




        if (isset($formData[$modelClass]['n__']))

        {

            foreach ($formData[$modelClass]['n__'] as $idx => $attributes)

            {

                $model = new $modelClass;

                $model->scenario = 'newMultimodel' //<------- add this

                $model->attributes = $attributes;




Now in the rules of your detail model you can use ‘on’=>‘newMultimodel’ to distinguish between standard validation and validation in the multimodelform.

If this helps, I will add the scenario line.

edit: as it turns out I’ll have to some up with my own solution because my situation is a little more complicated. I’ll be looking at your work for inspiration but you don’t need to add anything for my sake.

Cheers

[size=“1”]I guess it would add flexibility and give people a choice as to how they want to deal with scenarios. I think it would be useful but you have to judge to what extent what I’m doing is just stupid ;)

If you want me to test anything let me know. I may need a sample rule to really understand how it works though.

I think I can work with what I have now so if you’re busy don’t worry.

Thanks for your time![/size]

Auto-response: i use the title properties instead of tooltips.

Maybe it’s too late, but it may help someone… When you do the renderPartial you need to add a true in the end, indicating that the result must be returned instead of rendered…

Here its an example:




'Paso 4'=>$this->renderPartial('pasoCuatro',array('form'=>$form,'model'=>$model,'detalleVenta'=>$detalleVenta,'detallesValidos'=>$detallesValidos),true),



Hi Joblo,

I am using this extension and it’s great.

Is possible to use jquery in detail model ?

Example;

In detail model (Detall Bill)

I have one input text (Item Code), after to input this field I need to show How many items are in stock model

Any ideas?

Mr. Joblo

This is a great extension.

Sorry for my english.

I get autocomplete to work. May be you will had it to the next version.

What i notice :

1- .autocomplete() won’t apply on the clone input

2- .autocomplete() wont work unless the input field is added to the page

What i’ve done :

1- .autocomplete() won’t apply on the clone input

1-1 add a new input with same id and name

1-2 remove old input

1-3 apply .autocomplete() on new input




 public static function whenDoneAutoComplete($element, $idElement)

    {

        $options = isset($element['options']) ? $element['options'] : array();

        if (isset($element['sourceUrl']))

            $options['source'] = CHtml::normalizeUrl($element['sourceUrl']);

        else

            $options['source'] = $element['source'];


        $jsOptions = CJavaScript::encode($options);


	return "

		var acID=this.attr('id');

		if(acID.indexOf('".$idElement."')>=0)

		{

			var acName=this.attr('name');

			this.after('<input id=\"'+acID+'\" name=\"'+acName+'\"  type=\"text\">');

			this.remove();

			$('#'+acID).autocomplete(".$jsOptions.");

		}

	";

    }



But for this to work we have to give the name of the attribute to be treated ($idElement). So by now it will only work with one autocomplete. Need to review in order to handle an array of attributes name.

2- .autocomplete() wont work unless the input field is added to the page

2-1 add a new js options (jsWhenDone)

2-2 change jquery.relcopy.yii.3.3.js to handle whenDone new option




$(parent).find(rel+':last').after(clone);

$(clone).find('[id]').each(function(){

	funcWhenDone.call($(this));

});



At this step i ran into a strange issue. the autocomplete of the master template stop working after adding more copies although all the AC from copies work well.

To correct this i decided to use the master template only for cloning and change relcopy like this




/*if(!master.is(":visible"))

{

   master.show();

   mmfRecordCount++;

   return;

}*/


...


$(parent).find(rel+':last').after(clone);

$(clone).show();

$(clone).find('[id]').each(function(){

	funcWhenDone.call($(this));

});



Perhaps it isn’t the best way to do it but it works great for me.

I also change mmf to handle composit PK but do it qickly to get it working. May have some issues on delete with simple PK.

Hope this helps.

see attached files for complete changes

When I add a new row in tableView after a few rows option delete rows are duplicate???

Why is that?

Is anyone can help me?

Cheers.

Thanks you Elix for your "composite PK" add-on. It works very well.

Apparently, it is currently not possible to put several detail elements (eg, a Group of Members and Missions). The problem is that the widget can not distinguish the two elements in the number of records (if you add a member and a mission to a group, the widget think there are two members and two missions).

Has someone a solution for this problem?

Thanks for this extension.

I am having one issue, ‘Remove’ text is repeated as shown in attachment.

Can anyone please help?

3028

double.png

This is an issue if ‘tableView’=>true and ‘hideCopyTemplate’ => true.

I have to investigate this.

At the moment you have to set ‘hideCopyTemplate’ => false, if you use the tableView.

Thanks Joblo… It worked! +1 :)

Hi, i’m having troubles with dropdownlist in multimodelform.

When i load datas, i want that only some values appears for the user, in function of another field value.

This is partof my form:




<?php  

$model3 = RelStudentSemester::model()->findAll(array('condition'=>"group_id = " .  $model->group_id, 'order' => 'student_lastname, student_firstname'));

	$data3 = array();

	foreach ($model3 as $models){

		if($models->resit == 2)

		{

	 		$data3[$models->student_id] = array(2=>'Rattrapage');

		}

		elseif($models->resit == 1)

		{

	 		$data3[$models->student_id] = array(1=>'Hors rattrapage');

		}

		elseif($models->resit == 0)

		{

 		        $data3[$models->student_id] = array(0=>'Non renseigné');

		}

		else

		{

			$data3[$models->student_id] = array(3=>'A renseigner', 1=>'Hors rattrapage', 2=> 'Rattrapage');

		}

	} ?>

...


<?php   $relStudentSemesterFormConfig = array(

      'elements'=>array(

...

        'resit'=>array(

            'type'=>'dropdownlist',

		'items' => $data3,

        ),

...



When page load, i seee the correct value of the field (resit / rattrapage) but when i open the dropdownlist all values appears (see the attached file)…

Can someone help me with this?

Oups, i forgot the file…