Hi Tarjei,

did you find a solution, I am struggling with the same problem!

In my case I embed a form that embeds another form, and this second
one is not always needed. The tutorial of the advent calendar day 9
explains how to unset the submitted form, unfortunately un-setting
like in the post is not working for deeper nested forms (at least that
what it looks like).


Thanks in advance,
Christian


On Dec 11, 4:02 pm, Tarjei <tar...@scanmine.com> wrote:
> Hi,
>
> I'm trying to do something that is close to what is described 
> inhttp://www.symfony-project.org/advent_calendar/9/en.
>
> To start with, I'm using sfDoctrineGuard and I want to be able to
> define user accounts connected to either Persons or Firms.Person and
> Firm are two entities that both inherit a common Contact entity. I
> have added the schema I am using to the end of this message[1].
>
> In the ContactForm class I embedd a subclass[2] of
> sfGuardUserAdminForm into the form:
> // in configure()
> $form = new ContactUserForm($this->getObject()->user );
> if ($form->getObject()->id == null)  unset($form['id']);
> $this->embedForm("sfguarduser_id", $form);
>
> // and set up a postValidator.
> $this->mergePostValidator(new ContactUserValidatorSchema());
>
> I have also added a saveEmbeddedForms to the ContactForm class:
>   public function saveEmbeddedForms($con = null, $forms = null)
>   {
>       if (null === $forms)
>       {
>           $user = $this->getValue('sfguarduser_id');
>           if ($user == null) {
>           $forms = $this->embeddedForms;
>           unset($forms['sfguarduser_id']);
>           }
>
>       }
>       return parent::saveEmbeddedForms($con, $forms);
>   }
>
> The ContactUserValidatorSchema class is also attached, but I think
> this part of the setup is correctl.
>
> What I want to do is that if the username and password fields of the
> embedded schema are empty, then I do not want a user object to be
> created. As it stands now, an object is created with the username set
> to "". This only works the first time of course as the username has to
> be unique.
>
> I hope someone has an idea on what I am doing wrong here. I seem to
> have misunderstood something simple - I just wish I knew what.
>
> Tarjei
>
> 1. Schema:
>
> Contact:
>   columns:
>     firstname: string(255)
>     lastname: string(255)
>     jobRole: string(255) # stilling
>     mobile: string(255)
>     phone: string(255)
>     email: string(255)
>     description: string(255)
>     post_address_id: integer
>     sfguarduser_id: integer(4)
>   relations:
>     postAddress:
>       class: Address
>       type: one
>       foreignType: one
>       foreign: id
>       local: post_address_id
>       cascade: [delete]
>     user:
>       class: sfGuardUser
>       local: sfguarduser_id
>       cascade: [delete]
>
> Person:
>   inheritance:
>     extends: Contact
>     type: column_aggregation
>     keyField: type
>     keyValue: 2
>
> (There is also another subclass that I have omitted to make this post
> as short as possible).
>
> 2. class ContactUserForm extends sfGuardUserAdminForm {
>   public function configure() {
>     parent::configure();
>     foreach (array('username', 'password') as $key ) {
>       $this->validatorSchema[$key]->setOption('required', false);
>     }
>   }
>
> }
>
> 3.
> class ContactUserValidatorSchema extends sfValidatorSchema
> {
>   protected function configure($options = array(), $messages = array
> ())
>   {
>   }
>
>   protected function doClean($values)
>   {
>     $errorSchema = new sfValidatorErrorSchema($this);
>
>     /* return at once if the values are missing */
>     if (!isset($values['sfguarduser_id']) || !$values
> ['sfguarduser_id']) {
>       return $values;
>     }
>
>     $userFormValues = $values['sfguarduser_id'];
>
>     $errorSchemaLocal = new sfValidatorErrorSchema($this);
>
>     // username is filled but no password
>     if ($userFormValues['username'] && !$userFormValues['password'])
>     {
>       $errorSchemaLocal->addError(new sfValidatorError($this,
> 'required'), 'password');
>     }
>
>     // password is filled but no username
>     if ($userFormValues['password'] && !$userFormValues['username'])
>     {
>       $errorSchemaLocal->addError(new sfValidatorError($this,
> 'required'), 'username');
>     }
>
>     // no password and no username, remove the empty values
>     if (!$userFormValues['username'] && !$userFormValues['password'])
>     {
>       unset($values['sfguarduser_id']);
>     }
>
>     // some error for this embedded-form
>     if (count($errorSchemaLocal))
>     {
>       $errorSchema->addError($errorSchemaLocal, (string)
> "sfguarduser_id");
>     }
>
>     // throws the error for the main form
>     if (count($errorSchema))
>     {
>       throw new sfValidatorErrorSchema($this, $errorSchema);
>     }
>
>     return $values;
>   }
>
> }

--

You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-us...@googlegroups.com.
To unsubscribe from this group, send email to 
symfony-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en.


Reply via email to