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.

Archive /

Our old forums are still available as a read-only archive.

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

getCMSFields_forPopup validation


Reply


4 Posts   2187 Views

Avatar
ScottiouS

Community Member, 54 Posts

9 July 2008 at 5:15pm

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?

Avatar
dio5

Community Member, 501 Posts

10 July 2008 at 2:47am

I don't have an exact answer, but maybe you can use these things:

- LeftAndMain::ForceReload();
(see http://api.silverstripe.com/cms/core/LeftAndMain.html, look at the comment at the bottom)

- FormResponse: http://api.silverstripe.com/forms/core/FormResponse.html, which will output a message in the ajax way...

Hope this can help somehow...

Avatar
ScottiouS

Community Member, 54 Posts

10 July 2008 at 8:45am

Edited: 10/07/2008 9:26am

Thanks Dio5.. tried using FormResponse in the onBeforeWrite function but this doesn't return any value to the user. Perhaps I need to be returning values from elsewhere..

Avatar
ScottiouS

Community Member, 54 Posts

10 July 2008 at 10:56am

Ok, so here's my work around. Was the only way I could see to get information back to the user.

Basically
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.