Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Enumerates and ListboxField

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2837 Views
  • nomen
    Avatar
    Community Member
    52 Posts

    Enumerates and ListboxField Link to this post

    Hi all:

    I´m new to SilverStripe. Thanks for this pretty work!

    Based on the tutorial 2 "Extending a basic site" i´m trying to make a News page type.
    The difference between my news page type and the page type in this sample is that i need to have different news types.
    So, in the database definition i put this:

          static $db = array
          (
             'Date' => 'Date',
             'Author' => 'Text',
             'Type' => "Enum('Type1,Type2,Type3,General','General')"
          );


    It works good. The database is created without problems.

    The problem comes with the getCMSFields function.
    I need to select one of the news types when i write a new, so my getCMSFields looks like this:

    function getCMSFields()
    {
       $fields = parent::getCMSFields();

       $fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Date'), 'Content');
       $fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
       $fields->addFieldToTab('Root.Content.Main', new ListboxField('Type'), 'Content');

       return $fields;
    }


    I use ListboxField and, when i write a new news page i expect to have a combo with the options: Type1, Type2, Type3 and General. But i only have an empty combo.

    What i´m doing bad?

    Thanks in advance and excuse me for my poor english.

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Enumerates and ListboxField Link to this post

    Perhaps the $fields->addFieldToTab('Root.Content.Main', new ListboxField('Type'), 'Content'); needs more arguments, see: http://api.silverstripe.com/default/ListboxField.html

    Does this help?

  • nomen
    Avatar
    Community Member
    52 Posts

    Re: Enumerates and ListboxField Link to this post

    Hi SilverRay:

    Thanks for your answer.
    But if i don´t understand bad there is just one argument necessary in a list box (the name):

    ListboxField __construct (name $name, [title $title = ""], [source $source = array()], [value $value = array()], [form $size = null], [ $multiple = null], [ $form = null])

    I´m not right?

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Enumerates and ListboxField Link to this post

    this field doesn't auto-populate (we try to minimize the "magic" a bit to keep things easily debuggable *g*).

    i've updated the documentation of DropdownField (parent-class of ListboxField) to show an example which automatically gets the values of an Enum:
    http://doc.silverstripe.com/doku.php?id=dropdownfield

  • nomen
    Avatar
    Community Member
    52 Posts

    Re: Enumerates and ListboxField Link to this post

    Hi Ingo:

    Thank you, it works perfectly.

    Another option was to put like this:

    $fields->addFieldToTab("Root.Content.Main", new DropdownField( 'Type' , ''Type' ', array(
                'Type1' => 'Type1',
                'Type2' => 'Type2',
                'Type3' => 'Type3',
                'General' => 'General'
             )));

    The only problem is, as you say, that it doesn´t auto populate. If you change the enum you have remember to change the DropdownField, but this is not a problem for me.

    bye...

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Enumerates and ListboxField Link to this post

    "auto-populate" in the sense of loading a value: just add a fourth parameter from an existing object (usually $this->Type when you're in getCMSFields()), which will preselect the right entry

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: Enumerates and ListboxField Link to this post

    Hi Ingo,

    Is there are way to select the stored enum value from the list without adding it to the list of options?

    My item status select box looks a little strange with the following.

    Active
    Archived
    Active

    cheers.

  • Blackdog
    Avatar
    Community Member
    156 Posts

    Re: Enumerates and ListboxField Link to this post

    nevermind, I worked it out.

    static $db = array(
    'ItemStatus' => "Enum('Active, Archived','Active')"
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Main", new DropdownField( 'ItemStatus' , 'ItemStatus', array( 'Active' => 'Active', 'Archived' => 'Archived'),$this->ItemStatus));
    return $fields;
    }

    2837 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.