Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » TextField Default value doesn't work with db fields ?

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

Page: 1
Go to End
Author Topic: 2496 Views
  • Allisone
    Avatar
    Community Member
    27 Posts

    TextField Default value doesn't work with db fields ? Link to this post

    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

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: TextField Default value doesn't work with db fields ? Link to this post

    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

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: TextField Default value doesn't work with db fields ? Link to this post

    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.

  • guywatson
    Avatar
    Community Member
    15 Posts

    Re: TextField Default value doesn't work with db fields ? Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: TextField Default value doesn't work with db fields ? Link to this post

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

  • guywatson
    Avatar
    Community Member
    15 Posts

    Re: TextField Default value doesn't work with db fields ? Link to this post

    Thankyou very much, that works like a charm

    2496 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.