This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.2 » Problem with un-setting of embedded forms in bind()
Problem with un-setting of embedded forms in bind() [message #90274] Sun, 20 December 2009 21:15 Go to next message
cestcri  is currently offline cestcri
Messages: 232
Registered: August 2007
Location: Paris
Faithful Member
Hello and happy advent,


I spent my day figuring out how to dynamically add (and remove) embedded forms. Thanks to the new advent calendar (9th day: http://www.symfony-project.org/advent_calendar/9/en) this seems easier then never.

Unfortunately I did not get it to work properly. Here is my setup:

I have a form (item) that embeds a form (link) that embeds a form (place). This last place form is optional and might not be added. Depending on a user choice I want to either save the item/link pair with or without a place.

Here is my bind() method of the ItemForm class:

public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    if (isset($taintedValues['link']['is_international']))
    {
      // remove validators for ItemPlace
      $this->validatorSchema['link']['place']['name']->setOption('required', false);
      $this->validatorSchema['link']['place']->setPostValidator(new sfValidatorPass());

      unset(
        $this->embeddedForms['link']->embeddedForms['place'], 
        $taintedValues['link']['place']
      );
    }
    return parent::bind($taintedValues, $taintedFiles);
  }


If the setting is true (yes, it is, I checked Wink the validators are removed and the values plus the embedded place form un-setted.

Unfortunately the un-setting of $this->embeddedForms['link']->embeddedForms['place'] does not work. I found a hint to rather get and unset the nested embedded place form via reference...

$form = &$this->embeddedForms['link']->embeddedForms['place']);
unset($form);


... but this does not work either.


Hope that somebody has an idea, mine have all been tested negative.

Cheers,
Christian
SOLVED: Re: Problem with un-setting of embedded forms in bind() [message #96123 is a reply to message #90274 ] Wed, 24 March 2010 14:22 Go to previous messageGo to next message
cestcri  is currently offline cestcri
Messages: 232
Registered: August 2007
Location: Paris
Faithful Member
Just stumbling again over my post... here is my current solution, in case that somebody asks.


  public function bind(array $taintedValues = null, array $taintedFiles = null)
  {
    // remove place if "is international" checked
    if (isset($taintedValues['is_international']))
    {
      // tainted values still exist, so set validator to pass
      $this->validatorSchema['place'] = new sfValidatorPass();

      // remove embedded item place form
      unset(
        $this->embeddedForms['place']
      );

      // remove pointer, remember that ItemPlace will remain in database (ideally a flag would tell ItemLink to remove its ItemPlace and ItemLink's save())
      $this->object->setItemPlace(null);
    }

    return parent::bind($taintedValues, $taintedFiles);
  }


Enjoy.
Christian
Re: Problem with un-setting of embedded forms in bind() [message #96907 is a reply to message #90274 ] Wed, 07 April 2010 16:11 Go to previous messageGo to next message
pedro  is currently offline pedro
Messages: 37
Registered: April 2007
Location: Brasil - RJ
Member
I have a problem similar than yours..

I have 3 blanks embedded forms:


$todoWrapperForm->embedForm('tel_1', new TelefoneImovelForm());
$todoWrapperForm->embedForm('tel_2', new TelefoneImovelForm());
$todoWrapperForm->embedForm('tel_3', new TelefoneImovelForm());

$this->embedForm('tels', $todoWrapperForm);


And i have to remove the blank ones in bind() method. So i have the following code:

foreach($this->embeddedForms['tels']->getEmbeddedForms() as $key=>$telForm)
{
      if ($taintedValues['tels'][$key]['telefone'])
      {
        // only save todos that aren't blank
        $telForm->updateObject($taintedValues['tels'][$key]);
        $telForm->getObject()->save();
      } elseif (!$telForm->getObject()->isNew()) {
			
        // Remove embedded forms that are blanks here!!

      }
}




So, i have to remove $telForm from $this->embeddedForms['tels']->getEmbeddedForms().

Any suggestion?
Re: Problem with un-setting of embedded forms in bind() [message #96908 is a reply to message #96907 ] Wed, 07 April 2010 16:26 Go to previous messageGo to next message
cestcri  is currently offline cestcri
Messages: 232
Registered: August 2007
Location: Paris
Faithful Member
This should do it:

unset(
        $this->embeddedForms['tels_' . $key]->embeddedForms['place'], 
        $taintedValues['tels'][$key]
      );



Good luck...

Re: Problem with un-setting of embedded forms in bind() [message #101865 is a reply to message #90274 ] Thu, 01 July 2010 09:58 Go to previous messageGo to next message
johngeorge  is currently offline johngeorge
Messages: 1
Registered: July 2010
Location: London
Junior Member
I'm having exactly the same problem. I've used a similar approach and I can see that the forms are in fact getting removed, but they're still getting validated for some reason.

Here's my form class:

<?php

/**
 * IspolsonMotorCar form.
 *
 * @package    ispolson
 * @subpackage form
 * @author     John Wright
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class IspolsonMotorCarForm extends BaseIspolsonMotorCarForm
{
  /**
   * @see sfForm
   */
  public function configure()
  {
    $form = new IspolsonImageForm;
    $this->embedFormForEach('images', $form, 4);
  }
  
  /**
   * @see sfForm
   */
  public function bind(array $taintedValues=null, array $taintedFiles=null)
  {
    foreach ($this->getEmbeddedForm('images')->getEmbeddedForms() as $key => $imageForm) {
      if (!$taintedValues['images'][$key]['name'] && !$taintedValues['images'][$key]['alt'] && !$taintedFiles['images'][$key]['tmp_name']) {
        unset(
          $this->embeddedForms['images']->embeddedForms[$key],
          $taintedValues['images'][$key],
          $taintedFiles['images'][$key]
        ); 
      } 
    }
    parent::bind($taintedValues, $taintedFiles);
  }
}


Anyone got any ideas where I'm going wrong?

[Updated on: Thu, 01 July 2010 10:02]

Re: Problem with un-setting of embedded forms in bind() [message #102154 is a reply to message #101865 ] Wed, 07 July 2010 23:22 Go to previous message
eseceve  is currently offline eseceve
Messages: 1
Registered: July 2010
Location: chile
Junior Member

public function bind(array $taintedValues=null, array $taintedFiles=null)
  {
    foreach ($this->getEmbeddedForm('images')->getEmbeddedForms() as $key => $imageForm) {
      if (!$taintedValues['images'][$key]['name'] && !$taintedValues['images'][$key]['alt'] && !$taintedFiles['images'][$key]['tmp_name']) {
        unset(
          $this->embeddedForms['images'][$key],
          $this->validatorSchema['images'][$key],
          $taintedValues['images'][$key],
          $taintedFiles['images'][$key]
        ); 
      } 
    }
    parent::bind($taintedValues, $taintedFiles);
  }


Te faltó unset($this->validatorSchema['images'][$key]);

Saludos.
Previous Topic:what does automatic_cleaning_factor do?
Next Topic:propel:insert-sql doesn't work anymore since php 5.3
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software