I am creating CustomPage.php
and I would like to have certain fields in CMS form to be of "Options" or "RadioButton" or ... type
Is it possible?
Thank you in advance Victor
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
I am creating CustomPage.php
and I would like to have certain fields in CMS form to be of "Options" or "RadioButton" or ... type
Is it possible?
Thank you in advance Victor
Hi , welcome to Silverstripe !
Sure , no problem.
Have a look at :
http://doc.silverstripe.com/doku.php?id=form-field-types
and
http://doc.silverstripe.com/doku.php?id=data-types&s=field%20types
Thanks! But this documentation is a bit murky: Logically I would expect
Test.php:
<?php
class TestPage extends Page {
static $db = array(
'Foobar' => 'Optionset',
);
static $has_one = array(
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new OptionsetField(
$name = "Foobar",
$title = "FooBar's optionset",
$source = array(
"1" => "Option 1",
"2" => "Option 2",
"3" => "Option 3",
"4" => "Option 4",
"5" => "Option 5"
),
$value = "1"
);, 'Content');
return $fields;
}
}
class TestPage_Controller extends Page_Controller {
}
?>
return $fields;
}
}
class TestPage_Controller extends Page_Controller {
}
?>
Victor
You are confusing DB data field types : http://doc.silverstripe.com/doku.php?id=data-types&s=field%20types
with Form field types :
http://doc.silverstripe.com/doku.php?id=form-field-types
You can use a Text field to store your values.
Yes, I looked again and it is clear (unless I am still confused) that
'Foobar'=>'Text'
However simple correction does not work: db flush still returns an error. I need to put somewhere description of the OptionsetField .
Unfortunately I cannot find a sample code (which would also work for checkboxes and radiobuttons) and would greatly appreciate the sample code.
Victor
Here is a quick example...
static $db = array(
'Companies' => 'Text',
);
$fields->addFieldToTab('Root.Content.Companies', new OptionsetField('Companies', '', $DataObjectSet->toDropDownMap('id','name')));
Good luck !
Thanks, but I am still lost:
If I do simply like:
Test.php
<?php
static $db = array(
'Companies' => 'Text',
);
$fields->addFieldToTab('Root.Content.Companies', new OptionsetField('Companies', '', $DataObjectSet->toDropDownMap('id','name')));
?>
db flush does not generate a new page type even if I add
class TestPage_Controller extends Page_Controller {
}
but trying encapsulate the code in
<?php
class TestPage extends Page {
}
bring flush error
To work with Silverstripe, you need to be kiinda' comfortable with OO and MVC patterns ...
Also, please take a very good look at the tutorials....
Here is a more useful example....
class Blabla extends Page {
public static $db = array(
'Companies' => 'Text'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Companies', new OptionsetField('Companies', '', $DataObjectSet->toDropDownMap('id','name')));
return $fields;
}
}
class Blabla_Controller extends Page_Controller {
public function init() {
parent::init();
}