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   2885 Views

Avatar
nomen

Community Member, 52 Posts

9 January 2008 at 11:04pm

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

Community Member, 167 Posts

10 January 2008 at 12:48am

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

Community Member, 52 Posts

10 January 2008 at 3:16am

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

Forum Moderator, 801 Posts

12 January 2008 at 1:48pm

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

Community Member, 52 Posts

14 January 2008 at 10:53pm

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

Forum Moderator, 801 Posts

15 January 2008 at 8:39am

"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

Community Member, 156 Posts

16 April 2008 at 12:32am

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

Community Member, 156 Posts

16 April 2008 at 12:52am

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