0
votes

I have a many to many relation between Product and Properties. I'm using embedRelation() in my Product form to edit a Product and it's Properties. Properties includes images which causes my issue. Every time I save the form the updated_at column is updated for file properties even when no file is uploaded.

Therefore, I want to exclude empty properties when saving my form.

I'm using Symfony 1.4 and Doctrine 1.2.

I'm thinking something like this in my ProductForm.class.php, but I need some input on how to make this work.

Thanks

class ProductForm extends BaseProductForm
{
  public function configure()
  {
        unset($this['created_at'], $this['updated_at'], $this['id'], $this['slug']);

        $this->embedRelation('ProductProperties');
  }

    public function saveEmbeddedForms($con = null, $forms = null)
    {
      if (null === $forms)
      {
        $properties = $this->getValue('ProductProperties');
        $forms = $this->embeddedForms;

        foreach($properties as $p)
        {
            // If property value is empty, unset from $forms['ProductProperties']
        }
        }
    }

}
2

2 Answers

0
votes

I ended up avoiding Symfony's forms and saving models instead of saving forms. It can be easier when playing with embedded forms. http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/

0
votes

Solved it by checking if posted value is a file, and if both filename and value_delete is null I unset from the array. It might not be best practice, but it works for now.

Solution based on http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms

    class ProductPropertyValidatorSchema extends sfValidatorSchema
    {
      protected function configure($options = array(), $messages = array())
      {
            // N0thing to configure
      }

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

        foreach($values as $key => $value)
        {
          $errorSchemaLocal = new sfValidatorErrorSchema($this);

                if(array_key_exists('value_delete', $values))
                {
                    if(!$value && !$values['value_delete'])
                    {
                        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;
      }
    }