Orchard: Custom Registration fields
Asked Answered
T

2

5

For my Orchard project, I need some additional information from the user at registration time. (Say, First Name, Last Name, Pants Color). This information must be entered while registering and can not be deferred until later (as per client's orders).

I tried using the Profile and Extended Registration plugins to ask for those, but as far as I see, this only gives me optional fields to display in the registration form. Is there a way to present fields that are mandatory?

I also had a quick foray into overwriting the AccountController's Register method, as per this discussion, but I couldn't get it to work: The controller is in a different place, it can't be subclassed and even if I force it to, code is never executed. I presume they are using a much older version of Orchard.

So, in which direction should I go to create a mandatory field that is close to the Orchard philosophy? Should I create a new field type that rejects empty values maybe? (is that even possible)?

Typhus answered 7/6, 2011 at 10:12 Comment(0)
G
6

I wrote the ExtendedRegistration module because of that same need. You need to create a custom part, e.g.: MyRegistrationPart. Then you add that part to the User ContentType. In your part just add the [Required] attribute (Data annotations) to any properties that are mandatory. Registration will not succeed until those mandatory values have been filled out!

Hope it's clear now.

Gust answered 9/6, 2011 at 4:32 Comment(1)
Thanks, that's what I ended up doing. Oh and thanks a lot for writing that module.Typhus
T
3

While this probably won't answer your question just wanted to point out that it is my understanding that you don't need to override/subclass the AccountController class. Instead you need to "overwrite" the Users/Account/Register route by adding your own with a higher priority. To do that you need to implement an IRouteProvider as part of our module. Since it's an IDependency it will be loaded and processed automagically at run time. Something like:

public class Routes : IRouteProvider
{
    public void GetRoutes(ICollection<RouteDescriptor> routes)
    {
        routes.AddRange(GetRoutes());
    }

    public IEnumerable<RouteDescriptor> GetRoutes()
    {
        return new[] {
            new RouteDescriptor {
                // Make sure to be higher than the default
                Priority = ##### PRIORITY HERE (int) ######,
                Route = new Route(
                "Users/Account/Register",
                new RouteValueDictionary {
                    {"area", "#### YOUR MODULE AREA HERE ####"},
                    {"controller", "#### YOUR ACCOUNT CONTROLLER HERE ####"},
                    {"action", "#### YOUR REGISTER ACTION HERE ####"}
                },
                new RouteValueDictionary(),
                new RouteValueDictionary {
                    {"area", "#### YOUR MODULE AREA HERE ####"}
                },
                new MvcRouteHandler())
            }
        };
    }
}
Tso answered 7/6, 2011 at 13:44 Comment(2)
I can add that trying to actually override a controller method is a lot of hassle and probably not worth it. Today I learned...Typhus
Thanks Ivan! I used this method to "overwrite" the route to Users/Account/AccessDenied so I could handle the rendering of the login page myself. Also, it looks like Orchard 1.4 uses the path OrchardLocal/Users/Account/LogOn to handle the actual login.Putumayo

© 2022 - 2024 — McMap. All rights reserved.