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.

General Questions /

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

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

showing Boolean Value as YES or NO in Complex Table Field


Go to End
Reply


7 Posts   2280 Views

Avatar
Nicolaas

Forum Moderator, 220 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, 1837 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, 93 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, 220 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, 220 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.