@@ -154,7 +154,7 @@ public function renderUsing($formatterName, $attributes = array())
154154 * @param boolean $recursive False will prevent hidden fields from embedded forms from rendering
155155 *
156156 * @return string
157- *
157+ *
158158 * @see sfFormFieldSchema
159159 */
160160 public function renderHiddenFields ($ recursive = true )
@@ -235,6 +235,11 @@ public function bind(array $taintedValues = null, array $taintedFiles = null)
235235 $ this ->values = array ();
236236 $ this ->errorSchema = $ e ;
237237 }
238+
239+ if ($ this ->embeddedForms )
240+ {
241+ $ this ->bindEmbeddedForms ($ this ->taintedValues , $ this ->taintedFiles );
242+ }
238243 }
239244
240245 /**
@@ -247,6 +252,38 @@ protected function doBind(array $values)
247252 $ this ->values = $ this ->validatorSchema ->clean ($ values );
248253 }
249254
255+ /**
256+ * Bind embedded forms (recursivly)
257+ *
258+ * @param array $taintedValues
259+ * @param array $taintedFiles
260+ */
261+ public function bindEmbeddedForms (array $ taintedValues = null , array $ taintedFiles = null )
262+ {
263+ foreach ($ this ->embeddedForms as $ name => $ form )
264+ {
265+ $ form ->bind (
266+ isset ($ taintedValues [$ name ]) ? $ taintedValues [$ name ] : array (),
267+ isset ($ taintedFiles [$ name ]) ? $ taintedFiles [$ name ] : array ()
268+ );
269+
270+ $ this ->values [$ name ] = $ form ->getValues ();
271+
272+ $ widgetSchema = $ form ->getWidgetSchema ();
273+ $ decorator = $ widgetSchema ->getFormFormatter ()->getDecoratorFormat ();
274+ $ this ->widgetSchema [$ name ] = new sfWidgetFormSchemaDecorator ($ widgetSchema , $ decorator );
275+
276+ // keep widgetSchema synchronized
277+ $ form ->setWidgetSchema ($ this ->widgetSchema [$ name ]->getWidget ());
278+
279+ // update errorSchema
280+ if ($ form ->hasErrors ())
281+ {
282+ $ this ->errorSchema ->addError ($ form ->getErrorSchema (), $ name );
283+ }
284+ }
285+ }
286+
250287 /**
251288 * Returns true if the form is bound to input values.
252289 *
@@ -257,6 +294,37 @@ public function isBound()
257294 return $ this ->isBound ;
258295 }
259296
297+ /**
298+ * Directly updates form (and embbeded forms) values
299+ *
300+ * USE WITH CAUTION !!!!
301+ *
302+ * @param array $values
303+ */
304+ public function updateValues (array $ values )
305+ {
306+ $ this ->values = $ values + $ this ->values ;
307+
308+ $ this ->updateValuesEmbeddedForms ($ values );
309+ }
310+
311+ /**
312+ * Directly updates embedded form values
313+ *
314+ * @param array $values®
315+ */
316+ public function updateValuesEmbeddedForms (array $ values )
317+ {
318+ foreach ($ this ->embeddedForms as $ name => $ form )
319+ {
320+ if (isset ($ values [$ name ]))
321+ {
322+ $ form ->updateValues ($ values [$ name ]);
323+ $ this ->values [$ name ] = $ form ->getValues ();
324+ }
325+ }
326+ }
327+
260328 /**
261329 * Returns the submitted tainted values.
262330 *
@@ -385,64 +453,10 @@ public function embedForm($name, sfForm $form, $decorator = null)
385453 $ decorator = null === $ decorator ? $ widgetSchema ->getFormFormatter ()->getDecoratorFormat () : $ decorator ;
386454
387455 $ this ->widgetSchema [$ name ] = new sfWidgetFormSchemaDecorator ($ widgetSchema , $ decorator );
388- $ this ->validatorSchema [$ name ] = $ form ->getValidatorSchema ();
389-
390- $ this ->resetFormFields ();
391- }
392-
393- /**
394- * Embeds a sfForm into the current form n times.
395- *
396- * @param string $name The field name
397- * @param sfForm $form A sfForm instance
398- * @param integer $n The number of times to embed the form
399- * @param string $decorator A HTML decorator for the main form around embedded forms
400- * @param string $innerDecorator A HTML decorator for each embedded form
401- * @param array $options Options for schema
402- * @param array $attributes Attributes for schema
403- * @param array $labels Labels for schema
404- */
405- public function embedFormForEach ($ name , sfForm $ form , $ n , $ decorator = null , $ innerDecorator = null , $ options = array (), $ attributes = array (), $ labels = array ())
406- {
407- if (true === $ this ->isBound () || true === $ form ->isBound ())
408- {
409- throw new LogicException ('A bound form cannot be embedded ' );
410- }
411-
412- $ this ->embeddedForms [$ name ] = new sfForm ();
413-
414- $ form = clone $ form ;
415- unset($ form [self ::$ CSRFFieldName ]);
416-
417- $ widgetSchema = $ form ->getWidgetSchema ();
418-
419- // generate default values
420- $ defaults = array ();
421- for ($ i = 0 ; $ i < $ n ; $ i ++)
422- {
423- $ defaults [$ i ] = $ form ->getDefaults ();
456+ $ this ->validatorSchema [$ name ] = new sfValidatorPass ();
424457
425- $ this ->embeddedForms [$ name ]->embedForm ($ i , $ form );
426- }
427-
428- $ this ->setDefault ($ name , $ defaults );
429-
430- $ decorator = null === $ decorator ? $ widgetSchema ->getFormFormatter ()->getDecoratorFormat () : $ decorator ;
431- $ innerDecorator = null === $ innerDecorator ? $ widgetSchema ->getFormFormatter ()->getDecoratorFormat () : $ innerDecorator ;
432-
433- $ this ->widgetSchema [$ name ] = new sfWidgetFormSchemaDecorator (new sfWidgetFormSchemaForEach (new sfWidgetFormSchemaDecorator ($ widgetSchema , $ innerDecorator ), $ n , $ options , $ attributes ), $ decorator );
434- $ this ->validatorSchema [$ name ] = new sfValidatorSchemaForEach ($ form ->getValidatorSchema (), $ n );
435-
436- // generate labels
437- for ($ i = 0 ; $ i < $ n ; $ i ++)
438- {
439- if (!isset ($ labels [$ i ]))
440- {
441- $ labels [$ i ] = sprintf ('%s (%s) ' , $ this ->widgetSchema ->getFormFormatter ()->generateLabelName ($ name ), $ i );
442- }
443- }
444-
445- $ this ->widgetSchema [$ name ]->setLabels ($ labels );
458+ // keep widgetSchema synchronized
459+ $ form ->setWidgetSchema ($ this ->widgetSchema [$ name ]->getWidget ());
446460
447461 $ this ->resetFormFields ();
448462 }
@@ -463,7 +477,7 @@ public function getEmbeddedForms()
463477 * @param string $name The name used to embed the form
464478 *
465479 * @return sfForm
466- *
480+ *
467481 * @throws InvalidArgumentException If there is no form embedded with the supplied name
468482 */
469483 public function getEmbeddedForm ($ name )
0 commit comments