Ask your Symfony questions! Pay money and get answers fast! (more info)

Updating Specific Partials when they are within embedded form(s) Symfony


I am rendering a component inside an embedded form: here's the breakdown.
There are multiple images to a gallery page. Each image object has a component/partial that allows you to add tag(s) to that image.

So it looks like this
- Art
--_tagwidget (partial) - this partial is fed information specific from the Art embedded form its attached to

I got that to work without problem...BUT when I try to render/refresh the partial (after you add a tag) it updates EVERY iteration of that partial on the page

If there are 3 Art objects it updates ALL of the _tagwidget embedded into each one with the results of the query. I want to ONLY update the widget associated with that specific component/partial...does that make sense.

that way it doesnt update EVERY tagwidget partial on the page - if there was a way to do
<?php include_partial('managepage/tagwidget(1)', array('sidemenu' => $sidemenu)) ?>
<?php include_partial('managepage/tagwidget(2)', array('sidemenu' => $sidemenu)) ?>

etc... than I could target the partial directly, but I dont really see a way to approach this...maybe I am looking at it wrong?

Basically = Whats the best way to approach this when there are multiple iterations of the same partial on one page but just need to update ONE of them based on an action

UPDATE//////////Some code
Inside the gallery page I include the partial that lists all of the embedded forms
<?php include_partial('managepage/list', array('formgallery' => $formgallery,'form' => $form,'findid' => $findid,'siteid' => $siteid )) ?>

Inside the "_list" partial
I iterate through all embedded art "form" objects

Inside that loop I get a component that uses the object from the associated id of that embedded form within the loop
<?php foreach ($formgallery['Art'] as $i => $pForm):

<?php include_component('taggableComplete','tagWidget', array('object' => Master::getArtObject($anchor['id']))) ?>
<?php endforeach; ?>

Then I use js to submit the tag request in ajax

<input type="button" class="a-btn icon a-submit a-add" value="Add" onClick ="addTag('<?php echo get_class($object->getRawValue())?>','<?php echo $object->id; ?>')" />

function addTag($class, $id) {

$target = $class+$id;
$targetcontent = $("input."+$target).val();

$.post('<?php echo url_for("@default?module=managepage&action=addarttag") ?>', { tags:$targetcontent, object_id:$id});


return $this->renderComponent('taggableComplete', 'tagWidget', array('object' => $object));

This happens on success of adding a tag...BUT it renders EVERY tagWidget on the page with the results of that specific query...which replaces every embedded tag form with the results from that specific partial/component. I need a way to target JUST the partial/component thats being updated (even though they are technically all the same partial - the original include_component happens in the scope of a loop which is how I am able to render diff data for each one on page load - the widget obv also lists current tags)

I could prob re render the whole list partial that houses this data...but if there are 100 image objects on the page, thats a lot of queries I'd have to run every time you add even one tag (to just one Art item)

Hope that makes sense - im open to looking at this diff from a structure perspective, Im just not seeing the right solution.

Answers (3)


Milena Dimitrova answers:

You cannot do this:
<?php include_partial('managepage/tagwidget(1)', array('sidemenu' => $sidemenu)) ?>
<?php include_partial('managepage/tagwidget(2)', array('sidemenu' => $sidemenu)) ?>

but you can pass the object (or, if you prefer, just the object ID) to the partial as an argument and this will make every partial unique.

Also you can wrap each of the partials in an individual div with a unique ID:
<?php foreach ($objects as $object): ?>
<div id="tagwidget<?php echo $object->getId(); ?>">
<?php include_component('yourmodule' , 'tagwidget', array('object' => $object )) ?>
<?php endforeach ?>

Having these unique div IDs (such as tagwidget1, tagwidget2 etc) your Javascript function will be able to update exactly what you need - the last saved widget area:

function addTag($class, $id)
$target = $class+$id;
$targetcontent = $("#"+$target).val();
var params = {};
var ajax = new $.post('<?php echo url_for("@default?module=managepage&action=addarttag") ?>', params, function(response) {
$('#tagwidget'+$id).html(response); // update specific widget area

This Javascript function is one solution to the problem - you can see how it works here:
[[LINK href=""]][[/LINK]]

Another, more 'symfonic', solution would be to use the Javascript Ajax helper: form_remote_tag or jq_form_remote_tag (depending on your symfony version):
<?php echo jq_form_remote_tag(array(
'update' => 'widget'.$id,
'url' => 'default?module=managepage&action=addarttag',
'complete' =>'doSomethingElseFinallySuchAsHighlight()'
)) ?>

Wherever possible I prefer using Ajax helpers as it makes code less cluttered.

If you need further help please let me know.

Good luck!

leftrock comments:

Yea this actually makes a lot of sense...seems like I was prob depending too much on the partial helpers :) --- doing it with the "complete" function is a smart solution. Let me play with this for a little but I think your answer looks to be good!

leftrock comments:

I gave your response the full 10 says "voting in progress" Ill check back later to make sure the prize goes to you...thanks for your help!


Ben answers:

could you post some code? are you updating them with JavaScript, or reloading the page?

leftrock comments:

You can save ALL of the embedded forms at once with page load..but you cant include a <form> within a <form> I am handling the adding tags via js + action...added code below


Luis Cordova answers:

working on your solution

leftrock comments:

Awesome...look forward to hearing from you

Luis Cordova comments:

oh wait this is sf1.x, sorry i only do sf2.x

leftrock comments:

Bummer! Yea...I am moving new projects over to symfony 2 but I need to finish this one off in 1.x

Luis Cordova comments:

rather if you need me to convert this into sf2, please let me know, it depends on the project. PM me .