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 » support » symfony 1.3 and 1.4 » Dynamically Adding a new Form Element
Dynamically Adding a new Form Element [message #89214] Thu, 03 December 2009 18:18 Go to next message
rba1988  is currently offline rba1988
Messages: 4
Registered: November 2009
Junior Member
Hello. If for example I have the following tables:

person:
id: integer
name: string
// other person info here

sibling:
id: integer
person_id: integer <--foreign key to person
name: string
// other sibling info here

I have a module that can edit the profile of a person. But I want to include a part of the form where the user can add siblings. There would be an add button where he/she can add more siblings with their corresponding information. This means having to add more form elements that relate to the sibling table in conjunction with the form elements for editing the person's profile. How do I do it the symfony way?

Much help appreciated.Smile
Re: Dynamically Adding a new Form Element [message #89222 is a reply to message #89214 ] Thu, 03 December 2009 21:19 Go to previous messageGo to next message
gil_  is currently offline gil_
Messages: 14
Registered: June 2009
Junior Member
It's not exactly an answer to your request, but I read an article that may be used as a start point (it's like, for your example, if you are creating a single page to edit or delete all exesting sibling and/or add a new one in the same page)

Embedding Relations in Forms with Symfony 1.3 and Doctrine

May be used as a start point... Not totally sure, and you must use ajax - at least javascript/css to show/hide new sibling form : easy if you are using an array of elements - but how to link this form request to symfony, I don't know... But as it is possible to edit or delete existing ones, it should be possible to create new ones!)

I could be interested by a solution Smile (I'd developped something like that, but without symfony, and I'm a novice in the symfony world...)
Re: Dynamically Adding a new Form Element [message #89250 is a reply to message #89222 ] Fri, 04 December 2009 03:05 Go to previous messageGo to next message
rba1988  is currently offline rba1988
Messages: 4
Registered: November 2009
Junior Member
gil_ wrote on Thu, 03 December 2009 21:19

It's not exactly an answer to your request, but I read an article that may be used as a start point (it's like, for your example, if you are creating a single page to edit or delete all exesting sibling and/or add a new one in the same page)

Embedding Relations in Forms with Symfony 1.3 and Doctrine

May be used as a start point... Not totally sure, and you must use ajax - at least javascript/css to show/hide new sibling form : easy if you are using an array of elements - but how to link this form request to symfony, I don't know... But as it is possible to edit or delete existing ones, it should be possible to create new ones!)

I could be interested by a solution Smile (I'd developped something like that, but without symfony, and I'm a novice in the symfony world...)


I could get it to work with simple php scripts and javascript. But in the context of symfony, I'm not sure if they have developed such a mechanism for it. Hopefully in the near future.Smile

The only existing solution I can think of that involves doing it the symfony way is by creating a simple action that would add siblings one by one and edit the current siblings given a person. But that's too tedious and "manual" in nature.

Thanks for the reply. I will look into it.Very Happy
Re: Dynamically Adding a new Form Element [message #89446 is a reply to message #89214 ] Mon, 07 December 2009 05:23 Go to previous messageGo to next message
lionslair  is currently offline lionslair
Messages: 89
Registered: October 2007
Location: Perth / Western Australia
Member
I have done a lot of this sort of thing with symfony 1.0.x but do not know how I could port this to 1.4. The only way I can think of is that the form class would need to look for patterns of field names and within the class dynamically / virtually add the fields to the class so it does not fail when it comes to validation.


www.lionslair.net.au
Re: Dynamically Adding a new Form Element [message #92403 is a reply to message #89446 ] Thu, 28 January 2010 16:06 Go to previous messageGo to next message
pulse00  is currently offline pulse00
Messages: 25
Registered: June 2008
Junior Member
hi,

i'm trying to figure something like this too.
My goal is to embed forms dynamically via ajax,
so the user can add one-to-many relations without
an upper limit in the parent form.

It seems most of the tutorials out there about
that topic are for symfony <= 1.2, and won't really work with 1.3/1.4.

The one which is the basis for the following code is this here:

http://www.nacho-martin.com/dynamic-embedded-forms-in-symfon y

I'm really close to getting this to work, but i'm stuck at the last point, of binding
the values of the dynamic fields to the main form.

I'll post the important parts of the code, the
javascript is basically the same as in the link above:

Model (removed unneccessary stuff for readability):

Video:
  columns:
    title:              { type: string(255) }
    file_name:          { type: string(255) }
    is_active:          { type: boolean, notnull: true, default: true }

Track:
  columns:
    title:              { type: string(255), notnull: false }
    body:               { type: clob, extra: markdown }
    start_time:         { type: integer }
    end_time:           { type: integer }
    video_id:           { type: integer}
    is_active:          { type: boolean, notnull: true, default: true }    
  relations:
    Video:
      onDelete:         CASCADE
      foreignAlias:     Tracks


The VideoForm.class.php:

  public function configure()
  {
    parent::configure();
    unset(/*unset some fields*/);

    $this->validatorSchema['file_name'] = new sfValidatorFile(array(
      'required' => $this->isNew(),
      'max_size' => 536870912
    ));

    if ($this->isNew())
      $this->setDefault('is_active', true);

    $tracks = $this->getObject()->getTracks();

    // if there are no relations, add nothing to the embedded form holder
    if (!$tracks || !$tracks->count()) {
      $tracks = array();
    }
    
    // a form that holds the dynammically added ones
    $tracks_forms = new sfForm();
    // otherwise we get validation errors 
    $tracks_forms->getValidatorSchema()->setOption('allow_extra_fields', true);
    $count = 0;
    // embed existing relations
    foreach ($tracks as $track) {
      $track_form = new TrackForm($track);
      $tracks_forms->embedForm($count++, $track_form);
    }
    
    //embed the form-holder
    $this->embedForm('tracks', $tracks_forms, '<div id="foo">%content%</div>');
  }

  // adds a new embedded form to the embedded form holder (called via ajax from the action)
  public function addTrack($num) {
    $track = new Track();
    $track->setVideo($this->getObject());
    $track_form = new TrackForm($track);
    $this->embeddedForms['tracks']->embedForm($num, $track_form);
    $this->embedForm('tracks', $this->embeddedForms['tracks']);
  }

    
  public function doBind(array $values) {
    // re-create the embedded forms on binding, as otherwise there is no form
    // the values could be bound to
    foreach ($values['tracks'] as $i => $trackValue) {
      $track_form = new TrackForm();
      $this->embeddedForms['tracks']->embedForm($i, $track_form);
    }

    // here's the problem, the related records are created in the database,
    // with the correct foreign key to their parent object, but 
    // they have no values. For some reason, the parent::doBind($values)
    // call does not bind the values correctly to the embedded forms.
    parent::doBind($values);
  }



and the action, called via ajax to get the new embedded form:


  
  public function executeAddTrackForm(dmWebRequest $request) {
    
    $number = intval($request->getParameter('num'));
    
    if ($request->hasParameter('id')) {
      $video = Doctrine_Core::getTable('Video')->findOneById($request->getParameter('id'));
      $form = new VideoAdminForm($video);
    } else {
      $form = new VideoAdminForm();
    }
    $form->addTrack($number);
    return $this->renderPartial('addTrack', array('form'=>$form, 'num'=>$number));
  }



Has anyone a hint, why the values of the embedded forms are not bound correctly, hence having
NULL values in the database?
Re: Dynamically Adding a new Form Element [message #103401 is a reply to message #92403 ] Mon, 02 August 2010 11:06 Go to previous message
lazymanc  is currently offline lazymanc
Messages: 44
Registered: May 2007
Location: Manchester UK
Member
Hi - pulse00 did you ever work this out?

Previous Topic:"fake" properties on models
Next Topic:Drop Down Values from query
Goto Forum:
  

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