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.

We've moved the forum!

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

showing Boolean Value as YES or NO in Complex Table Field


Go to End


8 Posts   5156 Views

Avatar
Nicolaas

Forum Moderator, 224 Posts

6 January 2011 at 6:36pm

Hi

Stupid question - what is the easiest way to show a Boolean Value as YES / NO rather than 1 or 0 in a Complex Table Field????

Thank you

Nicolaas

Avatar
swaiba

Forum Moderator, 1899 Posts

7 January 2011 at 1:17pm

I'd use casting... like this...

class MyObject extends DataObject 
{ 
   static $db = array( 
      'MyBool'          => 'Boolean', 
   ); 
   public static $summary_fields = array ( 
      'MyBoolText'   => 'YesNo' 
   ); 
   static $casting = array( 
      'MyBoolText' => 'Text', 
   ); 
   public function MyBoolText(){
      return $this->MyBool ? 'Yes' : 'No'; 
  } 
}

Avatar
BenWu

Community Member, 97 Posts

15 June 2012 at 9:59pm

works. many thanks

Avatar
Nobrainer Web

Community Member, 138 Posts

22 June 2012 at 9:39am

I think it's possible to use $SomeField.Nice

Avatar
Nicolaas

Forum Moderator, 224 Posts

22 June 2012 at 9:06pm

Edited: 22/06/2012 9:07pm

Yes, you are probably right.

I think you need to use this:

table = ComplexTableField....

		$table->setFieldCasting(array(
			'MyBooleanField' => 'Boolean->Nice',
			'Total' => 'Currency->Nice'
		));

Avatar
Nicolaas

Forum Moderator, 224 Posts

22 June 2012 at 9:13pm

ok, so the question then is, how can we set these formatting rules for the summary_fields....

Avatar
copernican

Community Member, 189 Posts

23 June 2012 at 7:55am

I think this will work...

static $db = array(
     'myBool' => 'Boolean'
);

static $summary_fields = array(
     'myBool'
);

public function myBool(){
      return ($this->myBool==true ? 'Yes':'No');
}

It should then show Yes/No instead of 1/0.

Avatar
the_elliot

Community Member, 2 Posts

9 June 2016 at 1:07pm

Nice() will work on summary fields as well.

private static $db = array(
  'myBool' => 'Boolean'
);

private static $summary_fields = array(
  'myBool.Nice' => 'My boolean value'
);