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.

Form Questions /

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

CheckboxField bug


Go to End
Reply


3 Posts   999 Views

Avatar
amalet

Community Member, 7 Posts

14 February 2011 at 6:32am

Hello

I'm not sure where to report a bug so I'm posting it here in case anyone finds it useful.

It happens when transforming a CheckboxField to readonly.

The performReadonlyTransformation() method in CheckboxField creates a new CheckboxField_Readonly and passes the text value for 'Yes' or 'No' to the constructor:

function performReadonlyTransformation() {
	$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
	$field->setForm($this->form);
	return $field;	
}

The constructor then calls setValue(). However the setValue() method in CheckboxField_Readonly casts the value to an (int) and then tests it to set the value to either Yes or No.

class CheckboxField_Readonly extends ReadonlyField {
	//...
	
	function setValue($val) {
		$this->value = (int)($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
	}
}

Since the setValue() function is passed a string and casts it to an int, performReadonlyTransformation() always returns a field with the value 'No'.

Avatar
swaiba

Forum Moderator, 1837 Posts

14 February 2011 at 7:12am

http://open.silverstripe.org/ is where all the raised issues live :)

Avatar
amalet

Community Member, 7 Posts

14 February 2011 at 7:50am

Tried to submit 2 tickets on open.silverstripe.org but got errors after clicking 'Create ticket'.

There is also an bug in performReadonlyTransformation() in TreeDropdownField. The TreeDropdownField_Readonly that is created does not have a value. Since the constructor of TreeDropdownField does not take a parameter for the value it needs to be set by setValue().

function performReadonlyTransformation() {
	return new TreeDropdownField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
}

This object has no value. To correct this the value has to be set before returning the new object:

function performReadonlyTransformation() {
        $tdfro = new TreeDropdownField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
        $tdfro->setValue($this->value);
        return $tdfro;
}