I have a custom page which has a has_many relationship to a dataobject.
These objects are displayed in a getCMSFields HasManyComplexTableField.
I am running a function within onBeforeWrite() in the dataobject's class in order to validate a value which is entered before saving it to the DB. However if the value is not valid I have no way to notify the user. If I attempt to return any value from the onBeforeWrite function then it just hangs with the "saving..." button spinning.
How can I send a meaningful response back to the user?
Ok, so here's my work around. Was the only way I could see to get information back to the user.
1) create an extra field on your dataObject called Response
2) Add $fields->push( new ReadonlyField( 'Response', 'Response' ) ); to your getCMSFields_forPopup function.
3) Then based on your response you want to send set it in onBeforeWrite with $this->Response = "Error - blah blah";
This solution, although not ideal, works. The main problem is that the record is still saved. In my case this is not too much of a problem as I use the response field to filter whether or not to show an item on the front end of the CMS.
And since the popup box stays open it gives the user another chance to correct the problem and then save again.