0
votes

I have a doctrine form (KanoonGoodForm) based on KanoonGood model class, which looks like this:

class KanoonGoodForm extends BaseKanoonGoodForm
{
    public function configure()
    {
       $this->widgetSchema['count_num']->setAttribute('size', '1');
       $this->widgetSchema['price']->setAttribute('size', '1');

       $this->validatorSchema['category_id']->setOption('required', false);
       $this->validatorSchema['owner_id']->setOption('required', false);
       $this->validatorSchema['shop_id']->setOption('required', false);
       $this->validatorSchema['name']->setOption('required', false);
       $this->validatorSchema['price']->setOption('required', false);
       $this->validatorSchema['count_num']->setOption('required', false);
       $this->validatorSchema['description']->setOption('required', false);
    }
}

then I made KanoonGoodsGroupForm class, to have a form with arbitrary number of embedded KanoonGoodForm forms:

class KanoonGoodsGroupForm extends sfForm
{
    public function configure() 
   { 
       for ($i = 0; $i < 2; $i++) 
       {
           $this->embedForm('good_'.($i+1), new KanoonGoodForm());
       }
       $this->widgetSchema->setNameFormat('KanoonGoodsGroup[%s]');
       $this->mergePostValidator(new KanoonGoodValidatorSchema());
   }
}

In a module (named good), I echo an instance of KanoonGoodsGroupForm, when the new action is called:

public function executeNew(sfWebRequest $request)
{
    $this->form = new KanoonGoodsGroupForm();
}

Because all the embedded forms do not need to be filled, as THIS ARTICLE says, I made my own validator:

class KAnoonGoodValidatorSchema extends sfValidatorSchema
{
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('category_id', 'The category_id is required.');
    $this->addMessage('owner_id', 'The owner_id is required.');
    $this->addMessage('shop_id', 'The shop_id is required.');
    $this->addMessage('name', 'The name is required.');
    $this->addMessage('price', 'The price is required.');
    $this->addMessage('count_num', 'The count_num is required.');
    $this->addMessage('description', 'The description is required.');
  }

  protected function doClean($values)
  {
    $errorSchema = new sfValidatorErrorSchema($this);

    foreach($values as $key => $value)
    {
      $errorSchemaLocal = new sfValidatorErrorSchema($this);
      // no caption and no filename, remove the empty values
      if (!$value['name'] && !$value['price']&& !$value['count_num']&& !$value['description'])
      {
        unset($values[$key]);
      }

      // some error for this embedded-form
      if (count($errorSchemaLocal))
      {
        $errorSchema->addError($errorSchemaLocal, (string) $key);
      }
    }

    // throws the error for the main form
    if (count($errorSchema))
    {
      throw new sfValidatorErrorSchema($this, $errorSchema);
    }

    return $values;
  }
}

now I want to save those embedded forms which are filled by user, what should I do in my create action to accomplish this?

public function executeCreate(sfWebRequest $request)
{
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new KanoonGoodsGroupForm();
    $this->form->bind($request->getParameter('KanoonGoodsGroup'), $request->getFiles('KanoonGoodsGroup'));

    if ($this->form->isValid())
    {

         //what should I do here? to save those filled embedded forms?
    }

   $this->setTemplate('edit');
  }
3
Try foreach ($this->form->getEmbeddedForms() as $form) { $form->save(); } - 1ed
I tried that, but I got a 500 | Internal Server Error | sfValidatorErrorSchema, on this line: $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema); in my BaseKanoonGoodForm.class.php - Ehsan

3 Answers

2
votes

When I faced similar problem, I solved it with custom save() method in form which is collection of embedded forms like this

public function save($conn = null){
    foreach ($this->getEmbeddedForms() as $key => $form) {
        $object = $form->getObject();
        $object->merge($this->getValue($key));
        $object->save($conn);
    }
}

It walks through embedded forms and merges their objects with validated data from group form and then saves them

0
votes

You have to call:

$this->form->save();

But, you have to implement the part related to saveEmbeddedForms.

0
votes

You will have to Implement save methods for KanoonGoodsGroupForm as so:

public function save() {
    $this->updateObjectEmbeddedForms($this->getTaintedValues());
    $this->saveEmbeddedForms();
  }

  public function saveEmbeddedForms($forms = null) {
    if (null === $forms) {
      $forms = $this->embeddedForms;
    }

    foreach ($forms as $form) {
      if ($form instanceof sfFormObject) {
        $form->getObject()->save();
        $form->saveEmbeddedForms();
      } else {
        $this->saveEmbeddedForms($form->getEmbeddedForms());
      }
    }
  }


  public function updateObjectEmbeddedForms($values, $forms = null) {
    if (null === $forms) {
      $forms = $this->embeddedForms;
    }

    foreach ($forms as $name => $form) {
      if (!isset($values[$name]) || !is_array($values[$name])) {
        continue;
      }

      if ($form instanceof sfFormObject) {
        $form->updateObject($values[$name]);
      } else {
        $this->updateObjectEmbeddedForms($values[$name], $form->getEmbeddedForms());
      }
    }
  }