Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

TextField Default value doesn't work with db fields ?

Go to End

6 Posts   3630 Views


Community Member, 27 Posts

16 April 2009 at 12:24am

Edited: 16/04/2009 2:05am


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

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');
$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

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

Carbon Crayon

Community Member, 598 Posts

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 :)


Community Member, 27 Posts

16 April 2009 at 9:37am

Edited: 16/04/2009 9:38am

Hey aram. Thanks.
Thought that he might take the empty db value as the default value.
The defaults array proves it :D Thanks for the hint. Didn't know this array existed. It works like you said.


Community Member, 16 Posts

18 April 2011 at 5:54pm


I am having the same 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


Forum Moderator, 1391 Posts

18 April 2011 at 9:22pm

Edited: 18/04/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(){
	if (empty($this->ID)) $this->Brand = $this->Parent()->Title;


Community Member, 16 Posts

19 April 2011 at 11:15am

Thankyou very much, that works like a charm