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.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

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

Form with readonly-fields overwriting existing values


Go to End


3 Posts   2780 Views

Avatar
blueskies

Community Member, 44 Posts

14 March 2013 at 12:14am

HI all,

Hope someone can point me in the right direction with this problem. I have set up some forms in my front end to add/edit dataobjects. The idea is that with "add" all from fields are editable and with an "edit" only certain fields are editable. When I set some of the fields in the edit form to readonly, those fields get set to NULL/0 after the dataobject->write()... That is incorrect, they should be left alone and keep their original (add) value. This used to work in SS 2.* but readonly's get changed to 0/NULL when I upgraded to 3.

I have a form set up, the standard way:

public function EditForm() {			
		$fields = $this->currentRecord->getFrontendFields($this->request->param('OtherID'));		
		$validator = ($this->currentRecord->hasMethod('getValidator')) ? $this->currentRecord->getValidator() : new RequiredFields();
		$actions = new FieldList(
			new FormAction("doEdit", 'Opslaan')
		);		
		$form = new Form($this, "EditForm", $fields, $actions, $validator);
		$form->loadDataFrom($this->currentRecord);
		return $form;
	}

with a form handler:

function doEdit($data, $form, $request) {
		$form->saveInto($this->currentRecord);
		$this->currentRecord->write();
		return "#".$CSSClassName.$this->currentRecord->ID;
	}

When I add a dataobject, all form fields are editable. When a dataobject is edited, only certain fields can be changed. I handle that in the getFrontEndFields of the dataobject. Ie:

function getFrontendFields($actionGroup='edit') {
	$VerbruikKwhUrenHoog = new TextField('Verbruik Kwh Uren Hoog','Verbruik Kwh uren hoog(T1)');
	$VerbruikKwhUrenLaag = new TextField('Verbruik Kwh Uren Laag','Verbruik Kwh uren laag (T1)');
	if( !empty($this->ID) ){
		$VerbruikKwhUrenLaag = $VerbruikKwhUrenLaag->performReadonlyTransformation();	
    	}		
	return new FieldList(
		new FieldGroup(
			$VerbruikKwhUrenLaag,
			$VerbruikKwhUrenHoog 
		)			
	);
}	

Now after the $this->currentRecord->write() in the doEdit method all the readonly fields are set to 0/NULL. Is this expected behaviour since SS3? Or am I missing something?

Avatar
Willr

Forum Moderator, 5523 Posts

20 March 2013 at 8:39pm

Sounds like a 'feature' that wasn't documented in 2.* and has been removed. If a field is a Readonly field it shouldn't update the object. Perhaps raise a ticket to open.silverstripe.org.

Avatar
blueskies

Community Member, 44 Posts

19 April 2013 at 8:51am

Hmmm... I was afraid of that. Let me find a clean test-case and possibly raise a ticket.