C Lebeouf
C Lebeouf

Reputation: 1

WFFM MVC Field Not Highlighting When Failing CUstom Validation

We are using WFFM 2.4 Version 151103, Sitecore 7.2. Created custom validation for MVC form. When it returns a failed ValidationResult, the field on the form does not highlight and the error message is not displayed in the information block under the field. Here is the Model code:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Sitecore.Data.Items;
using Sitecore.Forms.Mvc.ViewModels;
using Sitecore.Forms.Mvc.ViewModels.Fields;
using Sitecore.Forms.Mvc.Validators;
using Frb.Atl.Extensions.WFFM.CustomValidators;

namespace Frb.Atl.Extensions.WFFM.Models
{
    public class TourSingleLineTextField : SingleLineTextField
    {
        [AtlantaTourParticipantsAttribute("tourProperty")]
        [DataType(DataType.Text)]
        public override string Value { get; set; }
    }
}

And the custom MVC validator:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Linq;
using Sitecore.Diagnostics;
using Sitecore.Forms.Mvc.Validators;
using Frb.Atl.Extensions.WFFM.Models;
using Sitecore.Forms.Mvc.Interfaces;
using Sitecore.Forms.Mvc.ViewModels;
using Sitecore.Forms.Mvc.Validators.Rules;
using Sitecore.Forms.Mvc.ViewModels.Fields;

namespace Frb.Atl.Extensions.WFFM.CustomValidators
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
    public class AtlantaTourParticipantsAttribute : DynamicValidationBase
    {

        public AtlantaTourParticipantsAttribute(string tourProperty)
        {
            Assert.ArgumentNotNullOrEmpty(tourProperty, "tourProperty");
        this.TourProperty = tourProperty;
        }

        public string TourProperty { get; set; }
        public string Participants { get; set; }
        public int MaxParticipants { get; set; }

        public string ErrorMessage = string.Empty;

        public const int MinParticipants = 10;

        public List<int> m60 = new List<int>(new int[] {12, 11, 10, 9, 5, 4, 3, 2, 1});

        protected override ValidationResult ValidateFieldValue(IViewModel TourSingleLineTextField, object value, ValidationContext validationContext)
        {
            var fieldModel = this.GetModel<TourSingleLineTextField>(validationContext);
            var tourSingleLineTextField = validationContext.ObjectInstance as TourSingleLineTextField;

            var Month = DateTime.Now.ToString("MM");
            int month = 0;
            int participants = 0;

            int.TryParse(Month, out month);
            int.TryParse(fieldModel.Value.ToString(), out participants);

            if (m60.Contains(month))
                MaxParticipants = 60;
            else
                MaxParticipants = 30;

            if (participants >= 10 && participants <= MaxParticipants)
            {
                return ValidationResult.Success;
            }
            else
            {
                ErrorMessage = "Number of participants must be between 10 and " + MaxParticipants.ToString() + ".";
                return new ValidationResult(ErrorMessage);
            }
        }
    }
}

Has anyone else experienced this behavior in WFFM? Any resolution if you have?

TIA

Upvotes: 0

Views: 126

Answers (1)

C Lebeouf
C Lebeouf

Reputation: 1

The cause of this issue was not in the custom field type or validation code. The problem was missing analytics config files. The form would fail before it ever got to the custom validation. Replaced the analytics files and the validation began working correctly.

Upvotes: 0

Related Questions