This project is read-only.

Custom error checking

Topics: Writing modules
Feb 18, 2011 at 8:32 PM
Edited Feb 18, 2011 at 8:33 PM


I have a content part that stores an API key. I want to check this is correct when they enter it by pinging the API.

I was trying to do this in the driver, like so:


protected override DriverResult Editor(MCSettingsPart settingsPart, IUpdateModel updater, dynamic shapeHelper)
	// Check API key is good
	if(!goodApi(settingsPart.ApiKey, true)
		updater.AddModelError("ApiKey", T("There was a problem with that API key."));
	else {
		updater.TryUpdateModel(settingsPart, Prefix, null, null);
	return Editor(settingsPart, shapeHelper);


The main problem with this is that settingsPart contains the value before it has been updated. So, I'm thinking that this is probably not the best place to have this sort of logic. What would be the best place to put it?


Richard Garside.

Feb 18, 2011 at 10:15 PM

This looks like validation and should probably be implemented as such.

Feb 19, 2011 at 4:18 PM

Okay. I've created a custom validation attribute and all seems well.

Is there an example in the code base that does this? I had a look but couldn't find any. I just want to check I'm doing it in the best way.


Feb 19, 2011 at 4:33 PM

Create a custom validation attribute (like the RequiredAttribute) and decorate your part's ApiKey property with it. Check this out on how to do it. Orchard will deal with validation automatically.

Feb 19, 2011 at 4:34 PM

What a timing:) Yes, afaik it's the right way to do it.