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.

Archive

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

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

Enumerates and ListboxField


Reply

8 Posts   2857 Views

Avatar
nomen

9 January 2008 at 11:04pm Community Member, 52 Posts

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.

Avatar
SilverRay

10 January 2008 at 12:48am Community Member, 167 Posts

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?

Avatar
nomen

10 January 2008 at 3:16am Community Member, 52 Posts

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?

Avatar
Ingo

12 January 2008 at 1:48pm Forum Moderator, 801 Posts

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

Avatar
nomen

14 January 2008 at 10:53pm Community Member, 52 Posts

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

Avatar
Ingo

15 January 2008 at 8:39am Forum Moderator, 801 Posts

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

Avatar
Blackdog

16 April 2008 at 12:32am Community Member, 156 Posts

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.

Avatar
Blackdog

16 April 2008 at 12:52am Community Member, 156 Posts

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