<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>A Web Developer from Israel &#187; Symfony</title>
	<atom:link href="http://israelwebdev.wordpress.com/category/symfony/feed/" rel="self" type="application/rss+xml" />
	<link>http://israelwebdev.wordpress.com</link>
	<description>A Tech Blog</description>
	<lastBuildDate>Sat, 24 Oct 2009 23:38:17 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='israelwebdev.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ad9f70e3d14e2fd490840ca309b18859?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>A Web Developer from Israel &#187; Symfony</title>
		<link>http://israelwebdev.wordpress.com</link>
	</image>
			<item>
		<title>How to Embed AJAX(!) Forms in Symfony 1.2 Admin Generator</title>
		<link>http://israelwebdev.wordpress.com/2009/05/04/how-to-embed-ajax-forms-in-symfony-12-admin-generator/</link>
		<comments>http://israelwebdev.wordpress.com/2009/05/04/how-to-embed-ajax-forms-in-symfony-12-admin-generator/#comments</comments>
		<pubDate>Mon, 04 May 2009 18:27:50 +0000</pubDate>
		<dc:creator>israelwebdev</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[1.2]]></category>
		<category><![CDATA[embedded]]></category>
		<category><![CDATA[forms]]></category>

		<guid isPermaLink="false">http://israelwebdev.wordpress.com/?p=53</guid>
		<description><![CDATA[Based on a few informative articles, helping a veteran Symfony 1.0 programmer navigate through the 1.2 form structure, I was able to embed forms in an admin generated form based on a one-to-many relationship. See those articles at:
How to Embed Forms in Symfony 1.2 Admin Generator
Interactive embedded forms
For the sake of speed, though, I wanted [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=israelwebdev.wordpress.com&blog=6466963&post=53&subd=israelwebdev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Based on a few informative articles, helping a veteran Symfony 1.0 programmer navigate through the 1.2 form structure, I was able to embed forms in an admin generated form based on a one-to-many relationship. See those articles at:</p>
<p><a href="http://sandbox-ws.com/how-to-embed-forms-in-symfony-12-admin-generator-part-2" target="_blank">How to Embed Forms in Symfony 1.2 Admin Generator</a><br />
<a href="http://redotheoffice.com/?p=42" target="_blank">Interactive embedded forms</a></p>
<p>For the sake of speed, though, I wanted to be able to add more embedded forms dynamically via AJAX before submitting the whole form. Here&#8217;s how I did it:</p>
<p>Adding the initial forms; on edit, load the related objects with a delete button; on new object or edit, add 3 blank embedded forms to start with.</p>
<pre><span><span class="comment">// lib/forms/PoForm.class.php</span><span class="keyword">
public</span><span> </span><span class="keyword">function</span><span> configure() {
</span></span>  sfLoader::loadHelpers(array('jQuery','Asset','Tag','Url'));
   $index = 0;
   foreach ($this-&gt;getObject()-&gt;getItemPos() as $book){
    $this-&gt;embedForm('item_pos'.++$index, new ItemPoForm($book));
    $label = "Item $index".jq_link_to_remote(image_tag('/sf/sf_admin/images/delete'), array(
      'url'     =&gt;  'po/deleteItem?idd='.$book-&gt;getId(),
      'success' =&gt;  "jQuery('.sf_admin_form_field_item_pos$index').remove();",
      'confirm' =&gt; 'Sure???',
    ));
    $this-&gt;widgetSchema-&gt;setLabel('item_pos'.$index,$label);
  }
  $a=sfContext::getInstance()-&gt;getUser()-&gt;getAttribute('N1added'.$this-&gt;getObject()-&gt;getId());
  $more = $this-&gt;getObject()-&gt;isNew()?max(3,$a-$index):($a&gt;($index+3)?$a-$index:3);
  for($i=0;$i&lt;$more;$i++){
    $ip = new ItemPo();
    $ip-&gt;setPo($this-&gt;getObject());
    $this-&gt;embedForm('item_pos'.++$index, new ItemPoForm($ip));
    $this-&gt;widgetSchema-&gt;setLabel('item_pos'.$index,"Item $index");
  }

  $label = "Item $index".jq_link_to_remote(image_tag('/sf/sf_admin/images/add'), array(
    'url'     =&gt;  'po/addItems?po='.$this-&gt;getObject()-&gt;getId().'&amp;index='.$index,
    'update'  =&gt;  'sf_fieldset_none',
    'position'=&gt;  'bottom',
  ));
  $this-&gt;widgetSchema-&gt;setLabel('item_pos'.$index,$label);

  sfContext::getInstance()-&gt;getUser()-&gt;setAttribute('N1added'.$this-&gt;getObject()-&gt;getId(), $index);
}</pre>
<p>The last item on our list gets an &#8220;add&#8221; image, which makes an AJAX call to add new blank forms.<br />
You can see we also use a user attribute to store how many embedded forms are being displayed, so we know what index we&#8217;re up to when we add more forms, and we know how many forms to regenerate on error.</p>
<p>Let&#8217;s create the add and delete actions-</p>
<pre>// apps/frontend/modules/po/actions/actions.class.php
public function executeDeleteItem(sfWebRequest $request) {
   $sub_category = ItemPoPeer::retrieveByPK($request-&gt;getParameter('idd'));
  if (!$sub_category) {return $this-&gt;renderText('Error '.$request-&gt;getParameter('idd'));}
  $sub_category-&gt;delete();
  if (!$this-&gt;getRequest()-&gt;isXmlHttpRequest()){
    $this-&gt;redirect('@po_edit?id='.$sub_category-&gt;getPo()-&gt;getId());
  }
  return $this-&gt;renderText('');
 }
 public function executeAddItems(sfWebRequest $request){
  $po = $request-&gt;getParameter('po');
  $index = sfContext::getInstance()-&gt;getUser()-&gt;getAttribute('N1added'.$po);
  sfLoader::loadHelpers(array('jQuery','Asset','Tag','Url'));
  $form = new PoForm();
  $form-&gt;setWidgets(array('id' =&gt; new sfWidgetFormInputHidden(),));
  $form-&gt;setValidators(array('id' =&gt; new sfValidatorPropelChoice(array('model' =&gt; 'Po', 'column' =&gt; 'id', 'required' =&gt; false)),));
  $widgetSchema = $form-&gt;getWidgetSchema();
  $widgetSchema-&gt;setNameFormat('po[%s]');

  for($i=0;$i&lt;3;$i++){
    $ip = new ItemPo();
    if($po) $ip-&gt;setPoId($po);
    $form-&gt;embedForm('item_pos'.++$index, new ItemPoForm($ip));
    $widgetSchema-&gt;setLabel('item_pos'.$index,"Item $index");
  }

  $label = "Item $index".jq_link_to_remote(image_tag('/sf/sf_admin/images/add'), array(
    'url'     =&gt;  'po/addItems?po='.$po.'&amp;index='.$index,
    'update'  =&gt;  'sf_fieldset_none',
    'position'=&gt;  'bottom',
  ));
  $widgetSchema-&gt;setLabel('item_pos'.$index,$label);

  sfContext::getInstance()-&gt;getUser()-&gt;setAttribute('N1added'.$po, $index);
  $this-&gt;form = $form;
  return $this-&gt;renderPartial('po/items');
 }</pre>
<p>The po/items partial is just a snippet from the auto-generated _form.php-</p>
<pre>&lt;?php foreach($configuration-&gt;getFormFields($form, 'new') as $fields): ?&gt;
 &lt;?php foreach ($fields as $name =&gt; $field): ?&gt;
 &lt;?php if ((isset($form[$name]) &amp;&amp; $form[$name]-&gt;isHidden()) || (!isset($form[$name]) &amp;&amp; $field-&gt;isReal())) continue ?&gt;
 &lt;?php include_partial('po/form_field', array(
  'name'       =&gt; $name,
  'attributes' =&gt; $field-&gt;getConfig('attributes', array()),
  'label'      =&gt; $field-&gt;getConfig('label'),
  'help'       =&gt; $field-&gt;getConfig('help'),
  'form'       =&gt; $form,
  'field'      =&gt; $field,
  'class'      =&gt; 'sf_admin_form_row sf_admin_'.strtolower($field-&gt;getType()).' sf_admin_form_field_'.$name,
 )) ?&gt;
 &lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;</pre>
<p>Finally, we modify our form&#8217;s bind method to remove unused embedded forms, and link the rest to our main object-</p>
<pre>// lib/forms/PoForm.class.php
  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    for($i=1;$i&lt;=sfContext::getInstance()-&gt;getUser()-&gt;getAttribute('N1added'.$this-&gt;getObject()-&gt;getId(),3);$i++)
    {
      if(!isset($taintedValues["item_pos$i"]) || empty($taintedValues["item_pos$i"]['item_id']))
      {
        $this-&gt;embeddedForms['item_pos'.$i]-&gt;getObject()-&gt;setDeleted(true);
        unset($this-&gt;embeddedForms["item_pos$i"],$this-&gt;validatorSchema["item_pos$i"]);
        unset($taintedValues['item_pos'.$i]);
      } else {
        $this-&gt;embeddedForms['item_pos'.$i]-&gt;getObject()-&gt;setPo($this-&gt;getObject());
      }

    }
    return parent::bind($taintedValues,$taintedFiles);
  }</pre>
<p>That should do it!<br />
(In case you&#8217;re having trouble seeing the code, you can go <a href="http://www.box.net/shared/2s7cp2el7n" target="_blank">here</a> to see it all)</p>
<div id="attachment_54" class="wp-caption alignnone" style="width: 460px"><img class="size-full wp-image-54" title="embedded forms image" src="http://israelwebdev.files.wordpress.com/2009/05/clipboard01.gif?w=450&#038;h=251" alt="symfony embedded forms ajax" width="450" height="251" /><p class="wp-caption-text">symfony embedded forms ajax</p></div>
<div id="attachment_55" class="wp-caption alignnone" style="width: 460px"><img class="size-full wp-image-55" title="edit form example" src="http://israelwebdev.files.wordpress.com/2009/05/clipboard02.gif?w=450&#038;h=319" alt="edit form example" width="450" height="319" /><p class="wp-caption-text">edit form example</p></div>
Posted in AJAX, jQuery, Symfony Tagged: 1.2, AJAX, embedded, forms, Symfony <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/israelwebdev.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/israelwebdev.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/israelwebdev.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/israelwebdev.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/israelwebdev.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/israelwebdev.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/israelwebdev.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/israelwebdev.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/israelwebdev.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/israelwebdev.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=israelwebdev.wordpress.com&blog=6466963&post=53&subd=israelwebdev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://israelwebdev.wordpress.com/2009/05/04/how-to-embed-ajax-forms-in-symfony-12-admin-generator/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/629016da336bf1f3d023751edb384c86?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">israelwebdev</media:title>
		</media:content>

		<media:content url="http://israelwebdev.files.wordpress.com/2009/05/clipboard01.gif" medium="image">
			<media:title type="html">embedded forms image</media:title>
		</media:content>

		<media:content url="http://israelwebdev.files.wordpress.com/2009/05/clipboard02.gif" medium="image">
			<media:title type="html">edit form example</media:title>
		</media:content>
	</item>
		<item>
		<title>Symfony ObjectHelper woes</title>
		<link>http://israelwebdev.wordpress.com/2009/02/12/symfony-objecthelper-woes/</link>
		<comments>http://israelwebdev.wordpress.com/2009/02/12/symfony-objecthelper-woes/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 16:15:30 +0000</pubDate>
		<dc:creator>israelwebdev</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[ObjectHelper]]></category>

		<guid isPermaLink="false">http://israelwebdev.wordpress.com/?p=32</guid>
		<description><![CDATA[Another dreaded case of Symfony&#8217;s white-screen of death struck on a customized Admin Generator -created page for editing. I got no on-screen or logged PHP errors. I tried doing some manual echo statements to track where the problem lay, but it just got me down the wrong path. Of course, the problem only occurred on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=israelwebdev.wordpress.com&blog=6466963&post=32&subd=israelwebdev&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Another dreaded case of Symfony&#8217;s white-screen of death struck on a customized Admin Generator -created page for editing. I got no on-screen or logged PHP errors. I tried doing some manual echo statements to track where the problem lay, but it just got me down the wrong path. Of course, the problem only occurred on my production server, but my almost identical development server showed the pages just fine.</p>
<p>Finally, I had a look at the Symfony logs, and noticed where the execution stopped short &#8211; right after a partial field that I was using to provide a drop-down category select. I was using the <em>object_select_tag</em> function, and removing that line fixed the problem. Thankfully, that function is an elegant wrapper for the <em>select_tag/objects_for_select</em> functions (the latter being a wrapper for the <em>options_for_select</em> function), and replacing the object function with its functional equivalent made things work fine.</p>
<p>Debugging is tiring!<br />
<span id="more-32"></span></p>
<p>The technical info:<br />
Symfony 1.0.17 (and post upgrade to 1.0.19)<br />
PHP 5.2.6 FreeBSD (my dev server which worked is 5.2.5 Windows , though I&#8217;m not sure this would make a difference)<br />
The options are being taken from a &#8220;virtual&#8221; DB class, as I created a special class for these categories which inherits from a more general class equipped with fields for id &amp; name, which was mostly all I needed for these categories.<br />
The category value is not located in the object&#8217;s table, but was created using my own Behavior class, which works quite well otherwise.<br />
Either of these 2 complications might have caused the problem, but I&#8217;m happy enough with the work-around to leave it alone.</p>
Posted in Symfony Tagged: ObjectHelper, Symfony <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/israelwebdev.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/israelwebdev.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/israelwebdev.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/israelwebdev.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/israelwebdev.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/israelwebdev.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/israelwebdev.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/israelwebdev.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/israelwebdev.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/israelwebdev.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=israelwebdev.wordpress.com&blog=6466963&post=32&subd=israelwebdev&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://israelwebdev.wordpress.com/2009/02/12/symfony-objecthelper-woes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/629016da336bf1f3d023751edb384c86?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">israelwebdev</media:title>
		</media:content>
	</item>
	</channel>
</rss>