Author: annis
Date: 2010-02-11 16:50:25 +0100 (Thu, 11 Feb 2010)
New Revision: 27899

Modified:
   plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/README
   
plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/lib/form/ahBaseFormDoctrine.class.php
   plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/package.xml
Log:
[ahDoctrineEasyEmbeddedRelationsPlugin] fixed a few bugs, so passing options to 
the embedded forms actually works; not necessary anymore to create a dedicated 
form to add the delete checkboxes, this is now handled by symfony's event 
dispatcher and the form.post_configure event :)


Modified: plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/README
===================================================================
--- plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/README  2010-02-11 
15:49:59 UTC (rev 27898)
+++ plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/README  2010-02-11 
15:50:25 UTC (rev 27899)
@@ -72,10 +72,10 @@
           'noNewForm' => true,
           'newFormLabel' => 'New repository!!!', 
           'newFormClass' => 'ahIntranetSubversionRepositoryNewForm',
-          'newFormClassArgs' => array('sf_user' => 
$this->getOption('sf_user')),
+          'newFormClassArgs' => array(array('sf_user' => 
$this->getOption('sf_user'))),
           'displayEmptyRelations' => false,
           'formClass' => 'ahIntranetSubversionRepositoryEmbeddedForm',
-          'formClassArgs' => array('sf_user' => $this->getOption('sf_user'))
+          'formClassArgs' => array(array('ah_add_delete_checkbox' => false, 
'another_form_option') => ...))
         ),
         '...' => array(
           ...
@@ -133,27 +133,43 @@
     
   * `newFormClass` (string, not required): the form class to use for the empty 
form
   
-  * `newFormClassArgs` (array, not required): form class options to pass to 
the empty form class on instantiation
+  * `newFormClassArgs` (array of arrays, not required): form class options to 
pass to the empty form class on instantiation.
+    Explanation for why it's an array of arrays: the way embedRelation works 
uses reflection to construct the right form objects. Now, the first argument is 
always the model object, the second is the form option array and the third is 
the local CSRF secret for the form.
+    You don't need to worry about the first one (the model object) because 
this is always null for the new form. If you want to pass some more options to 
the related form object (like the user object to avoid using sfContext) just 
follow the example above.
+    I could have changed this (because I implemented it) but I don't want to 
confuse the developer because when you look at the option `formClassArgs` below 
it's the same mechanism and I can't change that short of copying the whole 
embedRelation method over to the ahBaseFormDoctrine class, thereby losing the 
chance to automatically get upstream bugfixes. :)
   
   * `formClass` (string, not required): the form class to use for the existing 
related objects. 
-    If you want to use the embedded form on its own but also want to delete 
related objects inside this embedding form, create a new form (in the example 
above: ahIntranetSubversionRepositoryEmbeddedForm) extending the original 
embedded form class (in the example above: ahIntranetSubversionRepositoryForm) 
and add the following code:
+  
+  * `formClassArgs` (array of arrays, not required): form class options to 
pass to the existing related objects form class on instantiation
+    As of version 1.1 it's not necessary to create a separate form class for 
the existing related forms to display the delete checkbox.
+    This is now handled by the symfony event dispatcher. If you want to change 
how that works you can always copy over this method and change it to suit your 
needs:
     
         [php]
-        public function configure()
+        public function listenToFormPostConfigureEvent(sfEvent $event)
         {
-          parent::configure();
+          $form = $event->getSubject();
           
-          if ($this->object->exists())
+          if($form instanceof sfFormDoctrine && 
$form->getOption('ah_add_delete_checkbox', false) && !$form->isNew())
           {
-            $this->widgetSchema['delete_object'] = new 
sfWidgetFormInputCheckbox(array('label' => 'Delete'));
-            $this->validatorSchema['delete_object'] = new sfValidatorPass();
+            $form->setWidget('delete_object', new 
sfWidgetFormInputCheckbox(array('label' => 'Just destroy the damn object!')));
+            $form->setValidator('delete_object', new sfValidatorPass());
           }
         }
-      
-      When dealing with this inside of plugin classes you'll want to use the 
setup method for this.
+        
+    Or if you're like me and want to save space, here's another version you 
can use:
+    
+        [php]    
+        public function listenToFormPostConfigureEvent(sfEvent $event)
+        {
+          if ($form = parent::listenToFormPostConfigureEvent($event))
+          {
+            $form->widgetSchema['delete_object']->setOption('label', 'Just 
destroy the damn object!');
+          }
+        }
+    
+    This works because we're calling the plugin's event handler method.
+    This either returns the form so it added the delete checkbox and the 
validator and you can act on that or false. Neat and tidy. :)
   
-  * `formClassArgs` (array, not required): form class options to pass to the 
existing related objects form class on instantiation
-  
   * `displayEmptyRelations` (boolean, not required): set this to true (false 
is the default) if you want to check for existing related objects yourself. 
This can be done in the form template and is useful if you want to let the user 
know that 'There are no related repositories yet.'. The default is just not 
displaying anything in this case, which works for me. :)
 
 ## Questions, bugs, feature requests? ##

Modified: 
plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/lib/form/ahBaseFormDoctrine.class.php
===================================================================
--- 
plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/lib/form/ahBaseFormDoctrine.class.php
   2010-02-11 15:49:59 UTC (rev 27898)
+++ 
plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/lib/form/ahBaseFormDoctrine.class.php
   2010-02-11 15:50:25 UTC (rev 27899)
@@ -15,6 +15,9 @@
   public function embedRelations(array $relations)
   {
     $this->embedRelations = $relations;
+    
+    $this->getEventDispatcher()->connect('form.post_configure', array($this, 
'listenToFormPostConfigureEvent'));
+    
     foreach (array_keys($relations) as $relationName)
     {
       if (!isset($relations[$relationName]['noNewForm']) || 
!$relations[$relationName]['noNewForm'])
@@ -24,7 +27,7 @@
         $formArgs = !isset($relations[$relationName]['newFormClassArgs']) ? 
array() : $relations[$relationName]['newFormClassArgs'];
         $r = new ReflectionClass($formClass);
         
-        $newForm = $r->newInstanceArgs($formArgs);
+        $newForm = $r->newInstanceArgs(array_merge(array(null), 
array($formArgs)));
         $newForm->setDefault($relation->getForeignColumnName(), 
$this->object[$relation->getLocalColumnName()]);
         if (isset($relations[$relationName]['newFormLabel']))
         {
@@ -34,7 +37,7 @@
       }
       
       $formClass = !isset($relations[$relationName]['formClass']) ? null : 
$relations[$relationName]['formClass'];
-      $formArgs = !isset($relations[$relationName]['formClassArgs']) ? array() 
: $relations[$relationName]['formClassArgs'];
+      $formArgs = 
array_merge((!isset($relations[$relationName]['formClassArgs']) ? array() : 
$relations[$relationName]['formClassArgs']), 
array(array('ah_add_delete_checkbox' => true)));
       $this->embedRelation($relationName, $formClass, $formArgs);
       
       if (
@@ -45,8 +48,25 @@
         unset($this[$relationName]);
       }
     }
+    
+    $this->getEventDispatcher()->disconnect('form.post_configure', 
array($this, 'listenToFormPostConfigureEvent'));
   }
   
+  public function listenToFormPostConfigureEvent(sfEvent $event)
+  {
+    $form = $event->getSubject();
+    
+    if($form instanceof sfFormDoctrine && 
$form->getOption('ah_add_delete_checkbox', false) && !$form->isNew())
+    {
+      $form->setWidget('delete_object', new 
sfWidgetFormInputCheckbox(array('label' => 'Delete')));
+      $form->setValidator('delete_object', new sfValidatorPass());
+      
+      return $form;
+    }
+    
+    return false;
+  }
+  
   /**
    * Here we just drop the embedded creation forms if no value has been 
provided for them (this simulates a non-required embedded form),
    * please provide the fields for the related embedded form in the call to 
$this->embedRelations() so we don't throw validation errors

Modified: plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/package.xml
===================================================================
--- plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/package.xml     
2010-02-11 15:49:59 UTC (rev 27898)
+++ plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/package.xml     
2010-02-11 15:50:25 UTC (rev 27899)
@@ -12,7 +12,7 @@
   </lead>
   <date>2010-02-11</date>
   <version>
-    <release>1.0.1</release>
+    <release>1.1.0</release>
     <api>1.0.0</api>
   </version>
   <stability>
@@ -89,5 +89,22 @@
         Fixed README (typos, logic).
       </notes>
     </release>
+    <release>
+      <version>
+        <release>1.1.0</release>
+        <api>1.0.0</api>
+      </version>
+      <stability>
+       <release>stable</release>
+       <api>stable</api>
+      </stability>
+      <license uri="http://www.symfony-project.com/license";>MIT 
license</license>
+      <date>2010-02-11</date>
+      <license>MIT</license>
+      <notes>
+        - fixed a few bugs, so passing options to the embedded forms actually 
works.
+        - not necessary anymore to create a dedicated form to add the delete 
checkboxes, this is now handled by symfony's event dispatcher and the 
form.post_configure event. :)
+      </notes>
+    </release>
   </changelog>
 </package>

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to