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 » General discussion » How to prevent blank embed form objects being saved?
How to prevent blank embed form objects being saved? [message #102899] Wed, 21 July 2010 21:30 Go to next message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

Hi symfony guys!

I've been working on embedded forms for some days now, and I'm really getting desperate Sad

This is my story:

There is two tables: expense and comment. This have a 1:N relationship, where the user must be able to type a new comment each time the form is displayed. The comment is totally optional, except in some validation cases.

I have some code right now but I haven't found out yet what is the cause for this not to be working by now.

To learn from example, I follow the tutorial on the Advanced Forms chapter of the Do More With Symfony Book, and although there are some differences, like using doctrine instead of propel, and having a several embedded objects in the form, i managed to map my case to the example:

http://www.symfony-project.org/more-with-symfony/1_4/en/06-A dvanced-Forms#chapter_06_ignoring_embedded_forms

In order to not clutter this post with code and scare people away, I have sectioned and paste it using http://pastie.com, so it looks better and be easier to review.

This is the code I have right now.

http://pastie.org/1059450 -> model
http://pastie.org/1054052 -> main form
http://pastie.org/1054099 -> container form
http://pastie.org/1054095 -> validator used on container
http://pastie.org/1054079 -> form to embed

With all this, whenever I save the form with a comment both objects are saved and everything is great!!!, however, if no comment is typed , I get:

Propel Exception: Unable to execute INSERT statement...

on the comment object, which is weird because in the form this should be managed by the extension of the sfFormObject::saveEmbeddedForms() and the custom validator used.

Considering I based my code from the example in the documentation, something may be missing using Propel.

I hope someone can help me a little bit Smile

Best regards!

[Updated on: Sun, 25 July 2010 20:16]

Re: How to prevent blank embed form objects being saved? [message #103049 is a reply to message #102899 ] Sun, 25 July 2010 11:57 Go to previous messageGo to next message
xplo  is currently offline xplo
Messages: 428
Registered: September 2008
Faithful Member
isn t your expense table missing a primary key ?
also seing the full exception might help.

By the way what i think you re trying to code manually could be done in 1 line with propel 1.5 : http://propel.posterous.com/embed-relation-forms-in-one-line -with-sfprope
Re: How to prevent blank embed form objects being saved? [message #103060 is a reply to message #103049 ] Sun, 25 July 2010 20:29 Go to previous messageGo to next message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

Oops sorry, bad pastie... but actually the expense table has a primary key, i just missed it...

Propel 1.5 seems like the best and easier solution! I will look forward to upgrade it later... but for now, just yesterday i managed to figure it out, and now my message embedded form is working as it should Smile

Apparently the cause resided on how the expense object was set on the embedded comment object right after it was created.

In my form container I had:

<?php

class TnlExpenseCommentContainerForm extends sfForm
{
  public function configure()
  {
    ...
    for ($i = 0; $i < $this->getOption('size', 1); $i++)
    {
      $comment = new TnlExpenseComment();
      $comment->setTnlExpense($expense);
      ...

    }
    ...
  }
}


But after relearning about Propel basics once again, all needed was to change the set line

<?php

class TnlExpenseCommentContainerForm extends sfForm
{
  public function configure()
  {
    $expense = $this->getOption('expense');
    for ($i = 0; $i < $this->getOption('size', 1); $i++)
    {
      $comment = new TnlExpenseComment();
      $comment->setExpenseId($expense->getId());
      ...

    }
    ...
  }
}


It was something really dumb, but now thanks to that I understand a lot better how embed forms work and I hope this post helps someone else in the future >)

[Updated on: Wed, 28 July 2010 23:56]

Re: How to prevent blank embed form objects being saved? [message #103217 is a reply to message #103049 ] Wed, 28 July 2010 23:56 Go to previous message
jorge.chavez  is currently offline jorge.chavez
Messages: 34
Registered: May 2009
Location: Monterrey, México
Member

And just when I thought I've figured it out, my tests start crashing!

I'm still trying to avoid my blank comment embed form to be saved into the database.

Let's update my story to make it a little more documented:

I have 3 tables... expense, user and comment; I need to embed a new CommentForm into ExpenseForm, and this CommentForm should be optional except in some cases.

I have changed several things from the last version I tried. From start, I'm not using a container form since I thought that it wasn't really necessary to iterate over a container that only will have 1 element. Also, the validator class is applied as a post validator but for the ExpenseForm.

http://pastie.org/1064629--> model

http://pastie.org/1064637--> ExpenseForm

http://pastie.org/1064644--> validator

http://pastie.org/1064648--> CommentForm

Everything works excellent up to the point when I implement my saveEmbeddedForms() method and my validator. I am blocked right now and I'm not able to find the cause of this problem. But I may have a lead on how to set the relation between the parent and the embed object:

If I use :
$comment->setExpenseId()

and the expense form is new, symfony throws a propel exception (i supposed because the expense object gets an id after saving into db) but if i use:
$comment->setTnlExpense($expenseObject)

the expense form gets saved including the embed form (even if i unset the values in the validator)

I hope someone can lead me in the right direction Wink
Previous Topic:Symfony 1.4 Propel connection problem
Next Topic:Use i18n in backend
Goto Forum:
  

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