We can use the below code to create the registration for the user. I have created the following fields:
1. Full Name - Single Line Text
2. Email - Email
3. Password & ConfirmPassword - Confirm Password
4. Country - Dropdown
5. Submit button - submit
we will attach an action on the submit button that will execute the logic and register this user.
We are saving the form in the Sitecore forms DB.
using Foundation.Helper.Utility;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.Security.Accounts;
using System;
using System.Linq;
namespace Feature.Forms.FormActions
{
public class Registration : SubmitActionBase<string>
{
public Registration(ISubmitActionData submitActionData) : base(submitActionData)
{
}
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(data, nameof(data));
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
var fields = GetFormFields(formSubmitContext);
Assert.IsNotNull(fields, nameof(fields));
if (EmailOrPasswordFieldsIsNull(fields))
{
return AbortForm(formSubmitContext);
}
var result = Register(fields.Email, fields.Password, fields.FullName, Guid.NewGuid().ToString(), fields.Country);
if (!result)
{
return AbortForm(formSubmitContext);
}
return true;
}
protected virtual bool Register(string email, string password, string name, string profileId, string country)
{
Assert.ArgumentNotNullOrEmpty(email, nameof(email));
Assert.ArgumentNotNullOrEmpty(password, nameof(password));
try
{
var user = User.Create(Context.Domain.GetFullName(email), password);
user.Profile.Email = email;
if (!string.IsNullOrEmpty(profileId))
{
user.Profile.ProfileItemId = profileId;
user.Profile.SetCustomProperty(Constant.Country, country);
}
user.Profile.FullName = name;
user.Profile.Save();
}
catch (Exception ex)
{
Log.SingleError("Register user failed", ex);
return false;
}
return true;
}
private RegisterUserFormFields GetFormFields(FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
return new RegisterUserFormFields
{
Email = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Email"))),
Password = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("PasswordConfirmation"))),
FullName = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("FullName"))),
Country = Helper.GetDropdownValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Country")))
};
}
private bool EmailOrPasswordFieldsIsNull(RegisterUserFormFields field)
{
Assert.ArgumentNotNull(field, nameof(field));
return field.Email == null || field.Password == null;
}
private bool EmailOrPasswordsIsNull(RegisterUserFieldValues values)
{
Assert.ArgumentNotNull(values, nameof(values));
return string.IsNullOrEmpty(values.Email) || string.IsNullOrEmpty(values.Password);
}
private bool AbortForm(FormSubmitContext formSubmitContext)
{
formSubmitContext.Abort();
return false;
}
internal class RegisterUserFormFields
{
public string Email { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public string Country { get; set; }
public RegisterUserFieldValues GetFieldValues()
{
return new RegisterUserFieldValues
{
Email = Helper.GetValue(Email),
Password = Helper.GetValue(Password),
FullName = Helper.GetValue(FullName),
Country = Helper.GetValue(Country)
};
}
}
internal class RegisterUserFieldValues
{
public string Email { get; set; }
public string Password { get; set; }
public string FullName { get; set; }
public string Country { get; set; }
}
}
}
using System.Collections.Generic;
namespace Foundation.Helper.Utility
{
public class Helper
{
public static string GetValue(object field)
{
return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
}
public static string GetDropdownValue(object field)
{
var value = field?.GetType().GetProperty("Value")?.GetValue(field, null);
return ((List<string>)value)[0] ?? string.Empty;
}
}
}
The form looks like:
https://sitecorepeanuts.blogspot.com/2024/03/registration-form-using-sitecore-forms.html
ReplyDelete