5121 Posts in 1527 Topics by 1119 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1981 Views |
-
TextField Default value doesn't work with db fields ?

16 April 2009 at 12:24am Last edited: 16 April 2009 2:05am
Hi,
I use Eclipse + pdt to do my php stuff. There I could immidiately see, that there is a default value parameter for the TextField.
Only thing is, it doesn't work (as I hoped it would).I didn't understand why it wouldn't work, so I came looking in the forum. And I found this http://www.silverstripe.org/archive/show/85210
Well, and this was exactly the way I was trying to get a prefilled input field. Made me crazy. Till I just copy-pasted the code. And it worked. But why. Again it made me crazy, untill I just tried to use other names/parameters in my fields. And it worked, again It made me crazy, didn't know what was going on.
Now I know. If I do this
public static $db = array( 'Foo' => 'Text');
and
$fields->addFieldToTab('Root.Content.Main', new TextField("Foo","Bar","Test"),'Content');the Foo Input stays empty
but with this
$fields->addFieldToTab('Root.Content.Main', new TextField("notFoo","Bar","Test"),'Content');
it works and "Test" is written inside of the "notFoo" Input
SUMMARY
=========
So if the TextField "name" parameter is a key in the $db array, it doesn't work.
What to do?
Is there a workaround ? I mean, I need the value written into the db, so the name value must exist in the $db array, doesn't it?
Or is there another input type to use ?Thanks in advance and greetings from Germany
-
Re: TextField Default value doesn't work with db fields ?

16 April 2009 at 5:31am
Hi Allisone
I imagine this is because the value for the textfield is drawn from the DB, so if it's empty in the database then it will be empty in the field.
Instead use the $defaults array to set default db values:
static $defaults = array(
"foo" => "Test"
);That should do it
-
Re: TextField Default value doesn't work with db fields ?

16 April 2009 at 9:37am Last edited: 16 April 2009 9:38am
Hey aram. Thanks.
Thought that he might take the empty db value as the default value.
The defaults array proves it
Thanks for the hint. Didn't know this array existed. It works like you said. -
Re: TextField Default value doesn't work with db fields ?

18 April 2011 at 5:54pm
Hi
I am having the same problem......how can you do it if you want the value to be the parent title
e.g. this doesnt work
static $defaults = array(
'Brand' => $this->Parent()->Title
); -
Re: TextField Default value doesn't work with db fields ?

18 April 2011 at 9:22pm Last edited: 18 April 2011 9:23pm
That's because you can't use a class(instance) method in the declaration of a static array like that. There is the populateDefaults() method that lets you alter these array items as if they were dynamic vars:
public function populateDefaults(){
$this->Brand = $someDynamicVariable;
}Unfortunately that won't work in this particular case, since at the time this method is called, it seems the new page doesn't know about its parent yet. In this case you can try this:
function onBeforeWrite(){
parent::onBeforeWrite();
if (empty($this->ID)) $this->Brand = $this->Parent()->Title;
} -
Re: TextField Default value doesn't work with db fields ?

19 April 2011 at 11:15am
Thankyou very much, that works like a charm
| 1981 Views | ||
|
Page:
1
|
Go to Top |


