Front-end part validation

Topics: Writing modules
Apr 13, 2012 at 7:09 AM
Edited Apr 13, 2012 at 7:10 AM

Hello.

The following tutorial http://docs.orchardproject.net/Documentation/Writing-a-content-part explains the basics of creating a part, driver, shape and use of the Editor and Display methods from within a driver. When doing an Editor part for the shape we could use the DataAnnotations on the dashboard-side but what about the validation capabilities of the shape being displayed on the front-end using Display method ?

Basically what would do the trick is to make use of the @Html.ValidationMessage() which unfortunately does not work no matter what I do. 

I would really appreciate a helping hand with this one :)

Developer
Apr 13, 2012 at 7:21 AM

For me that helpers just works. What exactly are you trying to do? If you are building an editor you should use the Editor methods, not the Display. Or do you want to have a separate editor for the same part on the backend and on the frontend?

Apr 13, 2012 at 8:03 AM

I'm trying to create a simple editor for my front-side. Apparently I misunderstood the use of the Editor overrides. I thought that editor methods allowed me to display editor shape only on my dashboard - LOL.

So... this is what I have now:

Controllers
Controllers\AddressHandlingController.cs

Drivers
Drivers\AddressDisplayDriver.cs
Drivers\AddressEditDriver.cs

Models
Models\AddressDisplayPart.cs
Models\AddressEditPart.cs

Views
Views\Parts
Views\Parts\AddressDisplay.cshtml
Views\Parts\AddressEdit.cshtml

Module.txt
placement.info
Routes.cs
Migrations.cs

And what I should have is a one Driver (instead of two separate :) that will handle display and edit templates (both on the front-side). The driver would look like this than ?

public class AddressEditDriver : ContentPartDriver<AddressEditPart>
    {
        protected override DriverResult Display(AddressEditPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Address", () => shapeHelper.Parts_Address());
        }

        protected override DriverResult Editor(AddressEditPart part, dynamic shapeHelper) {
            return ContentShape("Parts_Address_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/AddressEdit", Model: part, Prefix: Prefix));
        }

        protected override DriverResult Editor(AddressEditPart part, IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }

 

 

Developer
Apr 13, 2012 at 8:23 AM

This looks fine to me and yes, then you should just use the standard editor methods. Be aware though that editors are often designed to be used in the backend only (or rather, to be used by administrators and editors), that means you may bump into issues properly setting user permissions. But if you make a simple content type this shouldn't be a real issue, but e.g. BodyPart or TagsPart is not meant to be used by ordinary users.

Also if you want a frontend editor AFAIK you'll need a custom controller and build your editor from there (IContentManager.BuildEditor() and friends).

Apr 13, 2012 at 9:40 AM
Edited Apr 13, 2012 at 11:52 AM

Ok. Let's sum this up so I would have a clear picture what is what here:

Driver with overloads:
Display method - for getting a shape (Views\template.cshtm) to display on the front end
Editor method - for getting a shape (Views\EditorTemplates\template.cshtm) to display on the dashboard

If I want to edit stuff I need to define a Routes.cs and MyController.cs. Within MyController I can get content to edit like so:

ContentItem contentItem = this.ContentManager.Get(id);
dynamic editor = this.ContentManager.BuildEditor(contentItem);
return new ShapeResult(this, shape);

but this would get me a shape of the whole page. For instance if the id=1 I'm getting site configuration shape.

I'm still confused as to how to get a shape from Drivers Editor method to use in my controller.

 

Developer
Apr 13, 2012 at 4:26 PM
Edited Apr 13, 2012 at 4:32 PM

"Editor method - for getting a shape (Views\EditorTemplates\template.cshtm) to display on the dashboard"

Yes, you should place the template into the EditorTemplates folder if you intend to create editor shapes the common way (how it's also in the tutorial). Now the editor of the content item could be displayed not just on the dashboard but from a custom controller too, as you've also used.

"If I want to edit stuff I need to define a Routes.cs and MyController.cs."

You only need to define routes if you're not satisfied with the standard one, e.g. MyModule/MyControllersName/MyAction.

"Within MyController I can get content to edit like so:"

This is correct.

"but this would get me a shape of the whole page. For instance if the id=1 I'm getting site configuration shape."

BuildEditor() creates the editor (that means, the content of an editor form, without the <form></form> itself) for the whole content item (unless you've defined editor groups like it's with site settings and e.g. Media settings). You can't simply build an editor for only one part, if that's what you'd like to do, but e.g. you could hide all the other editor shapes from the Placement.info (by using a match for the url of the action) or attach you part's editor to a new group and build the editor shape for only that group.