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

TextField Default value doesn't work with db fields ?


Reply

6 Posts   2569 Views

Avatar
Allisone

16 April 2009 at 12:24am (Last edited: 16 April 2009 2:05am), Community Member, 27 Posts

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

Avatar
Carbon Crayon

16 April 2009 at 5:31am Community Member, 598 Posts

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

Avatar
Allisone

16 April 2009 at 9:37am (Last edited: 16 April 2009 9:38am), Community Member, 27 Posts

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.

Avatar
guywatson

18 April 2011 at 5:54pm Community Member, 16 Posts

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

Avatar
martimiz

18 April 2011 at 9:22pm (Last edited: 18 April 2011 9:23pm), Forum Moderator, 1095 Posts

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;
}

Avatar
guywatson

19 April 2011 at 11:15am Community Member, 16 Posts

Thankyou very much, that works like a charm