Can i add a field which exist in database but in a hidden field in the form?H

Discussion relating to version 1.3.x and 1.4.x

Can i add a field which exist in database but in a hidden field in the form?H

Postby jacklkh » Fri Dec 04, 2009 6:26 am

Hello everyone, I'm new to symfony
I am having a problem that, i have a database which have field id, username, description.

I want a user to enter description and the username will be get from the login parameter.

My problem is that is that i need to hidden field in the form to store the username(and how can i do that?) or any other method i can do this?

thanks

(btw i'm using symfony 1.4 )

Jack =]
jacklkh
Member
 
Posts: 55
Joined: Fri Dec 04, 2009 6:23 am

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby rschumacher » Fri Dec 04, 2009 8:02 am

In your form's configure() method, you have three options:

(Assuming that your field is called 'username'.)

First option: use the sfWidgetFormInputHidden widget class for this field.
Code: Select all
$this->widgetSchema['username'] = new sfWidgetFormInputHidden();

Second option: call the widget's setHidden() method.
Code: Select all
$this->widgetSchema['username']->setHidden(true);

Third, and in my opinion least preferable option: set the widget's options directly:
Code: Select all
$this->widgetSchema['username']->setOption('is_hidden', true);
$this->widgetSchema['username']->setOption('type', 'hidden');

Hope this helps, cheers RAPHAEL
User avatar
rschumacher
Faithful Member
 
Posts: 231
Joined: Wed Aug 15, 2007 3:28 pm
Location: Basel, Switzerland

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby jacklkh » Fri Dec 04, 2009 2:57 pm

and how can i assign the value to that hidden field?
thanks

Jack
jacklkh
Member
 
Posts: 55
Joined: Fri Dec 04, 2009 6:23 am

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby jacklkh » Fri Dec 04, 2009 3:19 pm

say i wish to assign the username to the hidden field =]

Thanks

Jack
jacklkh
Member
 
Posts: 55
Joined: Fri Dec 04, 2009 6:23 am

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby jacklkh » Fri Dec 04, 2009 4:53 pm

found the solution

i create the form with new userForm(array(), array('username'=>$username)

then in the userForm configure function call
$username=$this->getOption("username");
$this->widgetSchema['username']->setAttribute('value',$username)

and will there be any easier solution?~

=]

Jack
jacklkh
Member
 
Posts: 55
Joined: Fri Dec 04, 2009 6:23 am

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby cokker » Fri Dec 04, 2009 6:56 pm

Do you use propel or doctrine objects? If so you can pass the user object to the form.

new userform($user-object);

additionally in your configure method you can use something like:$this->setDefault()

greets
Sven
cokker
Faithful Member
 
Posts: 591
Joined: Mon Jan 15, 2007 10:43 am
Location: Germany

Re: Can i add a field which exist in database but in a hidden field in the form?H

Postby jacklkh » Fri Dec 04, 2009 7:06 pm

i am using Doctrine =]

but i try to use set default function but it seems that not working
as a result i try to use setattribute

Jack
jacklkh
Member
 
Posts: 55
Joined: Fri Dec 04, 2009 6:23 am


Return to symfony 1.3 and 1.4

Who is online

Users browsing this forum: No registered users and 3 guests