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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Boolean Field


Reply

5 Posts   1805 Views

Avatar
kylehudson00

26 April 2011 at 1:20pm Community Member, 22 Posts

Hi, this question might have a really obvious answer, but I can't quite figure out how to do it! How would I go about getting a boolean field to display as 'Yes' or 'No' in the DOM list, as opposed to '1' or <blank>? I have it setup as a CheckboxField in my popup.

If anyone could point me in the right direction, I would be most appreciative!

Avatar
JonoM

26 April 2011 at 3:54pm Community Member, 103 Posts

Hi Kyle,

I do it this way - on your dataobject class add a function to convert the value to Yes or No, then refer to the function instead of the DB field when defining the DOM or in your $summary_fields.

i.e.

class MyObject extends DataObject
{
   static $db = array (
      'Title' => 'Varchar(200)',
      'Approved' => 'Boolean'
   );

   static $summary_fields = array(
      'Title' => 'Title',
      'ApprovedNice' => 'Approved?'
   );
   
   //Generate Yes/No for DOM / Complex Table Field
   public function ApprovedNice() {
      return $this->Approved ? 'Yes' : 'No';
   }
}

Cheers

Avatar
kylehudson00

26 April 2011 at 6:07pm Community Member, 22 Posts

That worked absolutely perfectly, thank you so much for the help!!!!

Avatar
UncleCheese

27 April 2011 at 9:50am 4085 Posts

FYI, this is built in. You just do $YourField.Nice.

Avatar
JonoM

27 April 2011 at 10:38am Community Member, 103 Posts

Doh! There's always an easier way in SilverStripe. Thanks for the tip Uncle Cheese. Still - can be useful in some situations, for instance I actually use that code to print out '***No*** Please review' to highlight records that need attention.