Hi,

You can also create 2 form classes : the first for authenticated users
(authenticatedUsersForm for example) and the other for non-logged
users (nonAutenticatedUsersForm for example).
class authenticatedUsersForm extends nonAutenticatedUsersForm

define your form widgets and validators in configure() of
nonAutenticatedUsersForm
then in configure() of authenticatedUsersForm call parent::configure()
and add the custom widget and validator for the additionnal field

That's it
Simply do an if in your action code

 if($this->getUser()->hasAttribute('user'))
    $form = new authenticatedUsersForm()
 else
  $form = new nonAutenticatedUsersForm()

Guillaume B.

On 9 sep, 15:40, torok84 <toro...@gmail.com> wrote:
> Thanks,
>
> this is what I was trying to do, but actually it is not very nice :).
> It didn't occur to me I could pass a parameter in the constructor.
>
> Paolo
>
> On Sep 8, 4:43 pm, Christopher Schnell <christopher.schn...@mda.ch>
> wrote:
>
> > hi,
>
> > you could do something nasty like
>
> > if(!$this->getUser()->hasAttribute('user'))
> >         $tempSchema=$this->form->getWidgetSchema();
> >       unset($tempSchema ['mine'];
> >       $this->form->setWidgetSchema($tempSchema);
>
> > }
>
> > but this wouldn't be testable.
>
> > better solution 
> > ishttp://eatmymonkeydust.com/2009/08/symfony-forms-flexible-widgets-bas...
>
> > Regards,
> > Christopher.
>
> > > -----Ursprüngliche Nachricht-----
> > > Von: symfony-users@googlegroups.com [mailto:symfony-
> > > us...@googlegroups.com] Im Auftrag von torok84
> > > Gesendet: Mittwoch, 8. September 2010 16:32
> > > An: symfony users
> > > Betreff: [symfony-users] Removing a field from a form in an action
>
> > > Hi,
>
> > > I have a form to select some search options. I have a field that I
> > > wanto to show only if the user is logged in. I want to do something
> > > like this:
>
> > > if(!$this->getUser()->hasAttribute('user'))
> > >       unset($this->form->widgetSchema['mine']);
>
> > > that doesn't work because widgetSchema is protected. Moreover doing
> > > this would leave a dangling validator for the 'mine' field. I
> > > undestand I can create a second form class without the 'mine' field,
> > > but it would be nice if I could simply unset a field.
>
> > > Thanks
> > > Paolo
>
> > > --
> > > If you want to report a vulnerability issue on symfony, please send it
> > > to security at symfony-project.com
>
> > > You received this message because you are subscribed to the Google
> > > Groups "symfony users" group.
> > > To post to this group, send email to symfony-users@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

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@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