Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Custom validation error messages using Umbraco properties

public class ContactViewModel
    {
        [UmbracoGetPropertyValueDisplayName("labelFirstName")]
        [UmbracoGetPropertyValueRequired("firstNameRequired")]
        [UmbracoGetPropertyValueRegularExpression("First Name", "errorOnlyAlphaNumericCharactersAllowed", "^[0-9a-zA-Z .]+$")]
        [UmbracoGetPropertyValueStringLength("First Name", "errorMessageStringLength", 5, MinimumLength = 1)]
        public string FirstName { get; set; }
        [UmbracoGetPropertyValueDisplayName("labelLastName")]
        [UmbracoGetPropertyValueRequired("lastNameRequired")]
        [UmbracoGetPropertyValueRegularExpression("Last Name", "errorOnlyAlphaNumericCharactersAllowed", "^[0-9a-zA-Z .]+$")]
        [UmbracoGetPropertyValueStringLength("Last Name", "errorMessageStringLength", 30, MinimumLength = 1)]
        public string LastName { get; set; }
        public string FirstNamePlaceholder { get; set; }
        public string LastNamePlaceholder { get; set; }
    }
@inherits UmbracoViewPage<Web.Core.ViewModels.ContactViewModel>
<h1>@TempData["Message"]</h1> 
@using (Html.BeginUmbracoForm("ContactForm", "ContactFormSurface", null, new {id = "frmContact", name = "frmContact", novalidate = "novalidate"}))
{
    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    @Html.AntiForgeryToken()
   
    <div class="form-group has-feedback">
        @Html.LabelFor(x => x.FirstName, new { @class = "control-label  sr-only" })
        @Html.TextBoxFor(x => x.FirstName, new { @class = "form-control", PlaceHolder = @Model.FirstNamePlaceholder })
    </div>
    <div class="form-group has-feedback">
        @Html.LabelFor(x => x.LastName, new { @class = "control-label sr-only" })
        @Html.TextBoxFor(x => x.LastName, new { @class = "form-control", PlaceHolder = @Model.LastNamePlaceholder })
    </div>
    <button type="Submit" id="btnContact">Send Message</button>
}
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using Web.Core.ViewModels;
namespace Web.Core.Controllers
{
    public class ContactFormSurfaceController : SurfaceController
    {
        [ChildActionOnly]
        public ActionResult DisplayForm()
        {
            string firstNamePlaceholder = PlaceholderFormText.GetPropertyValueItem("firstNamePlaceholder");
            string lastNamePlaceholder  = PlaceholderFormText.GetPropertyValueItem("lastNamePlaceholder");
            var model = new ContactViewModel
            {
                FirstNamePlaceholder    = firstNamePlaceholder,
                LastNamePlaceholder     = lastNamePlaceholder
            };
            return PartialView("~/Views/Partials/pvContactForm.cshtml", model);
        }
        [HttpPost, ValidateInput(true)]
        [ValidateAntiForgeryToken]
        public ActionResult ContactForm(ContactViewModel model)
        {
            if (!ModelState.IsValid)
            { 
                return CurrentUmbracoPage();
            }
            TempData["Message"] = "Done";
            return RedirectToCurrentUmbracoPage();
        }
    }
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //Needed if javascript disabled
            ErrorMessage = UmbracoPropertyValidationHelper.GetPropertyValueItem(_errorMessageKey);
            var removeRegex = ExtractStringBetweenTags.ExtractString(Pattern);
            
            var error           = FormatErrorMessage(validationContext.DisplayName);
            string errorMessage = StringBuilderMessage.Message(error, removeRegex);
            if (IsValid(value))
            {
                return ValidationResult.Success;
            }
            return new ValidationResult(_errorFieldName + " " + errorMessage + " " + "Javascript disabled");
        }

Please enter your comment.