Performing validation on part fields from a different part

Topics: Customizing Orchard, Writing modules
Apr 13, 2013 at 2:04 PM
I want to perform my own custom validation on the Autoroute part from another part (in my case, the part is called MinisiteBlob).

I want to do something like this in the updater version of the Editor() driver for MinisiteBlob. Basically I am checking whether the url entered in autoroute begins with baseurl:
var minisiteBaseUrl = minisiteBlobPart.BaseUrl;
autoroutePart = minisiteBlobPart.As<AutoroutePart<>();
if (autoroutePart != null)
{
    var displayBaseUrl = autoroutePart.DisplayAlias.Split('/').First();
    if (minisiteBaseUrl != displayBaseUrl) {
        updater.AddModelError("Blah blah this is my validation error");
    }
}
However... the autoroutePart has not yet been updated by its own driver. So I am dealing with old data. So clearly this is a bad approach!

Can anyone suggest a good approach to solve this problem? I'd rather not go modifying or copying the whole of the Autoroute module just to add a little validation rule.

Thanks!
Developer
Apr 13, 2013 at 3:04 PM
I don't know if there's a nice way to do this, but perhaps the following will at least work:
  1. In your updater method of the driver, set the minisiteBlobPart / content item into the HttpContext.Items dictionary.
  2. Create an ActionFilter. In the ActionExecuted method, retrieve the minisiteBlobPart from HttpContext.Items. If it has one, validate it. If validation fails, add a model error (you'll have access to the current controller via the context argument).
Apr 14, 2013 at 5:03 AM
Thanks, I follow for the most part... but could you please help me with how to access the updater from OnActionExecuted? I had a poke around in filterContext.Controller but I can't see anything.

Cheers
Developer
Apr 14, 2013 at 11:13 PM
Edited Apr 14, 2013 at 11:13 PM
Then you were already looking at it. It is hidden in plain sight: the Controller IS the updater. :)
Jul 18, 2013 at 7:59 PM
Hi guys!

For me it is really very strange.

Seems it is so common thing to create custom validations on server.
Why so difficult?

Thank you sfmskywalker for the idea.

Frankly I used it somewhat differently.
Just saved in HttpContext the whole updater reference on ContentPart Driver and use it in a ContentHandler.

Works fine!

But why on earth the updater is not reachable on ContentHandler?
It would solve all such wired obstacles.
Developer
Jul 19, 2013 at 5:23 PM
Custom validation is common, and it has been made very easy to do. The case from woodvorg, however, is in my experience not all that common.

The updater is available on ContentHandler when the UpdateEditor method is invoked.