views:

194

answers:

1

Hello, I been trying all day to do multiple insert for the same form based on a number and i couldn't go any where with it. I hope someone here would be help me out...

am using admin generator on 1.4 doctrine. I have a form which i generated with only two fields. what am trying to do is, based on a number inserted the form will be repeated x number of times.

In the generator file I added a partial which placed a text field in the beginning of the form with default value of 1. If I choose 2 the form below gets duplicated twice..

Here is what i did to my form.. In action

class prizesActions extends autoPrizesActions

{ public function executeNew(sfWebRequest $request) {

$this->form = $this->configuration->getForm(null, array('n' => 5));
$this->prizes = $this->form->getObject();

} }

and in the PrizesForm, I wrote the following

class PrizesForm extends BasePrizesForm

{ public function configure() { $array = $this->getOptions(); for ($i = 0; $i < $array['n']; $i++) {

    $this->setWidgets(array(
      'id'         => new sfWidgetFormInputHidden(),
      'prize_no'   => new sfWidgetFormInputText(),
      'prize'      => new sfWidgetFormInputText(),
      'created_at' => new sfWidgetFormDateTime(),
      'updated_at' => new sfWidgetFormDateTime(),
    ));

    $this->setValidators(array(
      'id'         => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'prize_no'   => new sfValidatorInteger(array('required' => false)),
      'prize'      => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'created_at' => new sfValidatorDateTime(),
      'updated_at' => new sfValidatorDateTime(),
    ));

    $this->widgetSchema->setNameFormat('prizes['.$i.'][%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
}





unset(  $this['updated_at'],
        $this['created_at']
        );  

} }

I think the loop is working but its over writing the widgets at every entry and i cannot find other method to append instead. Any ideas?

Thanks,

+1  A: 

Did you try embedForm()? Code below should give you an idea.

class PrizesForm extends BasePrizesForm
{
  public function configure() 
  { 
    $this->setWidgets(array(
      'id'         => new sfWidgetFormInputHidden(),
      'prize_no'   => new sfWidgetFormInputText(),
      'prize'      => new sfWidgetFormInputText(),
      'created_at' => new sfWidgetFormDateTime(),
      'updated_at' => new sfWidgetFormDateTime(),
    ));

    $this->setValidators(array(
      'id'         => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'prize_no'   => new sfValidatorInteger(array('required' => false)),
      'prize'      => new sfValidatorString(array('max_length' => 200, 'required' => false)),
      'created_at' => new sfValidatorDateTime(),
      'updated_at' => new sfValidatorDateTime(),
    ));

    $this->widgetSchema->setNameFormat('prizes[%s]');
  }
}

class PrizesGroupForm extends sfForm
{
  public function configure() 
  { 
    $array = $this->getOptions();

    for ($i = 0; $i < $array['n']; $i++) 
    {
      $this->embedForm('prizes_' . $i, new PrizesForm());
    }

    $this->widgetSchema->setNameFormat('prizes_group[%s]');
  }
}
kuba