OptionsetField

This field allows you to ensure that a form element is submitted is not optional and is part of a fixed set of data. This field uses the input type of radio. It's a direct subclass of DropdownField, so the constructor and arguments are in the same format.

:optionsetfield.png

Example use

If you want to implement a OptionsetField into a form element, you need to pass it an array of source data.

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

You can use the helper functions on data object set to create the source array. eg:

//Database request for the object
$myDoSet = DataObject::get("FooBars","");
if($myDoSet){
 // This returns an array of ID => Title
 $map = $myDoSet->toDropDownMap();
 
  // Instantiate the OptionsetField 
  $fieldset = new Fieldset(
    new OptionsetField(
     $name = "Foobar",
     $title = "FooBar's optionset",
     $source = $map,
     $value = $map[0]
    )
  );
}
 
// Pass the fields to the form constructor. etc

API documentation

 
optionsetfield.txt · Last modified: 2010/05/05 23:38 (external edit)

Please use comments for notes, tips and corrections about the described functionality.
Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Creative Commons LicenseSilverStripe Documentation by SilverStripe is licensed under a Creative Commons Attribution 3.0 New Zealand License.
Based on a work at doc.silverstripe.com
Recent changes RSS feed Driven by DokuWiki