Skip to content

Conversation

@bjarnef
Copy link
Contributor

@bjarnef bjarnef commented Jul 12, 2022

The method GetPostFieldValue(Form form, HttpContext context, string key) need HttpContext passed in as second parameter.

Furthermore there is no e at use of GetPostField() method - it takes Form as first parameter.

Not sure how the docs should be handled regarding .NET5 vs .NET6.

In Umbraco 10 / Umbraco Forms 10 GetPostField() returns a possible nullable Field:

using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core.Events;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Services.Notifications;

namespace MyProject.Core.Notifications
{
    /// <summary>
    /// Catch form submissions before being saved and perform custom validation.
    /// </summary>
    public class FormValidateNotificationHandler : INotificationHandler<FormValidateNotification>
    {
        public void Handle(FormValidateNotification notification)
        {
            // If needed, be selective about which form submissions you affect.
            if (notification.Form.Name == "Form Name")
            {
                // Check the ModelState
                if (notification.ModelState.IsValid == false)
                {
                    return;
                }

                // A sample validation
                var email = GetPostFieldValue(notification.Form, notification.Context, "email");
                var emailConfirm = GetPostFieldValue(notification.Form, notification.Context, "verifyEmail");

                // If the validation fails, return a ModelError
                if (email.ToLower() != emailConfirm.ToLower())
                {
                    var field = GetPostField(notification.Form, "verifyEmail");
                    if (field != null)
                    {
                        notification.ModelState.AddModelError(field.Id.ToString(), "Email does not match");
                    }
                }
            }
        }

        private static string GetPostFieldValue(Form form, HttpContext context, string key)
        {
            Field? field = GetPostField(form, key);
            if (field == null)
            {
                return string.Empty;
            }

            return context.Request.HasFormContentType && context.Request.Form.Keys.Contains(field.Id.ToString())
                ? context.Request.Form[field.Id.ToString()].ToString().Trim()
                : string.Empty;
        }

        private static Field? GetPostField(Form form, string key) => form.AllFields.SingleOrDefault(f => f.Alias == key);
    }
}

Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes up the errors found.

@eshanrnh
Copy link
Contributor

Thanks for the review, @AndyButland 🙌 Merging it in.

@eshanrnh eshanrnh merged commit 259f531 into umbraco:main Jul 13, 2022
@bjarnef bjarnef deleted the patch-26 branch July 13, 2022 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants