(Newbie) Silverstripe 4 : boolean field value only saves to true

(Newbie)

Hi,

For some reason, whenever I save a StudentPage entry in the backend, boolean fields such as ‘Active’ (see below) are always saved with a value of 1 (i.e: true) no matter what. I tried replacing the value of 1 and 0 (below) by true and false, same problem. I had initially defined $db = [ ’ Active’ => ‘Boolean(1)’ , // …] but I do not think it is relevant.

Any idea ?

Thanks.

K.

(StudentPage.php)

	private static $db = [
		'Active' => 'Boolean',
                // ...
        ];
	public function getCMSFields()
	{
		$fields = parent::getCMSFields();
		$fields>addFieldToTab('Root.Main',DropdownField::create('Active','Active user',['True' => 1,'False' => 0 ]),'Content');
        // ...		
	}

Couldn’t find the solution in the documentation for SS4, but it is here:

https://stackoverflow.com/questions/49915593/silverstripe-3-use-dropdown-yes-no-for-a-boolean-db-field-in-the-cms

Hi

The source array for your DropdownField is the wrong way around. The array key should be the field value (ie. 1 or 0) and the array value is the name of the selection you want to display.

Try setting the array as [1 => 'True', 0 => 'False']

Thanks for your help, @kaftka, you are right.

I just didn’t know the two options a boolean field can take in the CMS have to be defined with strings (string data type).