Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Rowan Easter 5 posts 75 karma points
    Jul 22, 2020 @ 14:20
    Rowan Easter
    0

    Extending RegisterModel, in PureLive mode

    Hi All, I'm trying to implement my own surface controller to handle member registration with an extra field called IsValidated

    I'm in PureLive mode

    I've tried to follow a number of tutorials, including these: https://our.umbraco.com/documentation/reference/templating/modelsbuilder/Understand-And-Extend https://24days.in/umbraco-cms/2016/getting-started-with-modelsbuilder/ https://our.umbraco.com/Documentation/Reference/Templating/Mvc/Forms/tutorial-child-action

    I've tried to extend RegisterModel with a file in ~Models/Membership/RegisterModel.cs

       using System;
    using System.ComponentModel.DataAnnotations;
    using Umbraco.Web.Models;
    
    
    
    //Needs to be in same namespace to extend
    //https://our.umbraco.com/forum/templates-partial-views-and-macros/80378-extend-model-using-partial-class#comment-288095
    namespace Umbraco.Web.Models
    { 
        public partial class RegisterModel
        {
    
            /// <summary>
            /// Is member's email validated
            /// </summary>
            public Boolean IsValidated { get; set; }
        }
    }
    

    And have just copied the umbRegisterController from the umbraco source code, adding my own ChildAction to render the form ~Controllers/CustomerMembershipController.cs

    using System;
    using System.Web.Mvc;
    using System.Web.Security;
    using Umbraco.Core;
    using Umbraco.Core.Cache;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Persistence;
    using Umbraco.Core.Services;
    using Umbraco.Web.Models;
    using Umbraco.Web.Mvc;
    
    
        public class CustomerMembershipController : SurfaceController
        {
            public CustomerMembershipController()
            {
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            [ValidateUmbracoFormRouteString]
            public ActionResult Register([Bind(Prefix = "registerModel")] RegisterModel model)
            {
                if (ModelState.IsValid == false)
                {
                    return CurrentUmbracoPage();
                }
    
                // U4-10762 Server error with "Register Member" snippet (Cannot save member with empty name)
                // If name field is empty, add the email address instead
                if (string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Email) == false)
                {
                    model.Name = model.Email;
                }
    
                var test = model.IsValidated;
    
                MembershipCreateStatus status;
                var member = Members.RegisterMember(model, out status, model.LoginOnSuccess);
    
                switch (status)
                {
                    case MembershipCreateStatus.Success:
    
                        TempData["FormSuccess"] = true;
    
                        //if there is a specified path to redirect to then use it
                        if (model.RedirectUrl.IsNullOrWhiteSpace() == false)
                        {
                            return Redirect(model.RedirectUrl);
                        }
                        //redirect to current page by default
    
                        return RedirectToCurrentUmbracoPage();
                    case MembershipCreateStatus.InvalidUserName:
                        ModelState.AddModelError((model.UsernameIsEmail || model.Username == null)
                            ? "registerModel.Email"
                            : "registerModel.Username",
                            "Username is not valid");
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        ModelState.AddModelError("registerModel.Password", "The password is not strong enough");
                        break;
                    case MembershipCreateStatus.InvalidQuestion:
                    case MembershipCreateStatus.InvalidAnswer:
                        // TODO: Support q/a http://issues.umbraco.org/issue/U4-3213
                        throw new NotImplementedException(status.ToString());
                    case MembershipCreateStatus.InvalidEmail:
                        ModelState.AddModelError("registerModel.Email", "Email is invalid");
                        break;
                    case MembershipCreateStatus.DuplicateUserName:
                        ModelState.AddModelError((model.UsernameIsEmail || model.Username == null)
                            ? "registerModel.Email"
                            : "registerModel.Username",
                            "A member with this username already exists.");
                        break;
                    case MembershipCreateStatus.DuplicateEmail:
                        ModelState.AddModelError("registerModel.Email", "A member with this e-mail address already exists");
                        break;
                    case MembershipCreateStatus.UserRejected:
                    case MembershipCreateStatus.InvalidProviderUserKey:
                    case MembershipCreateStatus.DuplicateProviderUserKey:
                    case MembershipCreateStatus.ProviderError:
                        //don't add a field level error, just model level
                        ModelState.AddModelError("registerModel", "An error occurred creating the member: " + status);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
    
                return CurrentUmbracoPage();
            }
    
            [ChildActionOnly]
            public ActionResult ShowCommentForm()
            {
                return PartialView("RegisterForm");
            }
    
        }
    

    With this, the RegisterModel I created seems to override the default one generated by Umbraco, so Visual studio shows that model.IsValidated is available, but the rest of the default fields are not.

    Any ideas please?

  • Robert J. Bullock 386 posts 405 karma points
    Jul 23, 2020 @ 16:44
    Robert J. Bullock
    0

    Would like to know on this as well. Membership functionality and code examples are very scarce for 8.

Please Sign in or register to post replies

Write your reply to:

Draft