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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Returning a custom error in ModelAdmin


Go to End


5 Posts   3117 Views

Avatar
AdamJ

Community Member, 145 Posts

26 May 2011 at 12:51am

I'm have an onBeforeWrite function that if a checkbox is checked in that class, it checks some criteria in the relationships and if that returns in the negative, blocks the save and returns an error message using the following:

user_error('Products can not be marked for sale without active variants', E_USER_ERROR);
exit();

From Firebug I can see that this line works as it returns the ajax save request with the error and a code of 500 Error. What I want to do though is have that error displayed to the user in the little javascript notification in the bottom left corner of the panel that default actions use. Is there a way to hook into that?

Avatar
swaiba

Forum Moderator, 1899 Posts

26 May 2011 at 1:11am

Edited: 28/03/2013 12:01pm

Here is how I do custom validation within ModelAdmin... hope it helps...

class MyDataObject extends DataObject {
...
	function getCMSValidator() {
		return new MyDataObject_Validator();
	}
...
}

class MyDataObject_Validator extends RequiredFields {
	function php($data) {
		$bRet = parent::php($data);

		if (empty($data['FieldName'])) {
			$this->validationError('FieldName','FieldName is required',"required");
			$bRet = false;
		}

		return $bRet;
	}
}

Avatar
JonoM

Community Member, 130 Posts

28 March 2013 at 7:13am

Thank you Swaiba! This helped me a lot today. I just had to change '$obj' to '$this' to get it to work.

Avatar
swaiba

Forum Moderator, 1899 Posts

28 March 2013 at 7:24am

No Problem...

One other tip is to add this as well this then covers the validators use in a CTF popup too

function getValidator() {
	return new MyDataObject_Validator();
}

Avatar
JonoM

Community Member, 130 Posts

28 March 2013 at 7:46am

Ah sweet, cheers!