Copied to clipboard

Flag this post as spam?

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


  • Chris Wilson 100 posts 377 karma points
    Mar 27, 2015 @ 13:23
    Chris Wilson
    0

    Umbraco Forms - FieldType Setting value is null during ValidateField step

    The back-story:

    I'm running an Umbraco 7.2.1 site with Umbraco Forms 4.0.2 and I'm attempting to make my first custom field to attach to an existing form (created through the back-office).

    The purpose of the control is to take entry of an email address and verify that the address domain is not on a blacklist.

    I have a set of domains which are always blacklisted (temporary email providers for example) and the rest I wish to gather on a form-to-form basis by using a Forms Setting. The forever blacklisted domains are stored in a Config Setting and set in the constructor, the form-specific blacklisted domains are set in the back-office when the control is added.

    When stepping through the Constructor I can see that the EmailAddressesToIgnore property is null, it then receives the value from the Config Setting and this value persists.

    When stepping through the ValidateField method I can see that the value is set to the Config Setting just as it was after the constructor phase.

    If I then go back into the back-office, the values set on the control are still present in the textbox, so somehow the values I set there are not available on the property.


    Tl;dr:

    Having added a simple text Setting to a custom FieldType, any data entered into this Setting is visible and stored in the back-office but not available when debugging the ValidateField method.

    Have I made an incredibly simple mistake?

    public class UniqueEmail : FieldType
    {
        [Setting("Ignored email domains",
                 description = "Enter email address domains which should be ignored, separate each with a comma. NOTE: You should only enter the value after the @ e.g. domain.co.uk",
                 view = "uniqueemail")]
        public string EmailAddressesToIgnore { get; set; }
    
        public UniqueEmail()
        {
            //Provider
            Id = new Guid("D6A2C406-CF89-11DE-B075-55B055D89593 ");
            Name = "Unique Email Address";
            Description = "Renders a html input fieldKey"; //FieldType
            Icon = "icon-autofill";
            DataType = FieldDataType.String;
            SupportsRegex = true;
    
            // Pre-populate the field
            EmailAddressesToIgnore = string.IsNullOrWhiteSpace(EmailAddressesToIgnore)
                ? Properties.Settings.Default.Competitions_IgnoredEmailDomains.ToLower()
                : EmailAddressesToIgnore + "," + Properties.Settings.Default.Competitions_IgnoredEmailDomains.ToLower();
        }
    
        public override IEnumerable<string> ValidateField(Field field, IEnumerable<object> postedValues, HttpContextBase context)
        {
            // Perform the base validation first (lightest)
            postedValues = postedValues.ToList();
            var validationResults = base.ValidateField(field, postedValues, context).ToList();
            if (validationResults.Any()) return validationResults;
    
            // Perform ignored domain validation next (heavier)
            if (!string.IsNullOrWhiteSpace(EmailAddressesToIgnore))
            {
                validationResults.AddRange(from emailAddress in postedValues
                                           select emailAddress.ToString().ToLower().Split('@')
                                               into emailSections
                                               where emailSections.Count() == 2 &&
                                                     EmailAddressesToIgnore.SplitAndCleanCsvIntoList().Contains(emailSections[1])
                                               select "Email address cannot belong to an employee or temporary email service");
    
                if (validationResults.Any()) return validationResults;
            }
    
            return validationResults;
        }
    }
    
  • Chris Wilson 100 posts 377 karma points
    Mar 27, 2015 @ 17:42
    Chris Wilson
    0

    Having checked out the implementation of this kind of functionality on Nibble (http://www.nibble.be/?p=89) it seems that I've done it correctly.

    Would someone mind weighing in on this?

    Kind regards, Chris

  • Chris Wilson 100 posts 377 karma points
    Apr 01, 2015 @ 09:55
    Chris Wilson
    0

    Anyone?

    It definitely seems like Settings for Custom Fields are almost completely broken in Umbraco Forms!

  • Chris Wilson 100 posts 377 karma points
    Apr 10, 2015 @ 11:20
    Chris Wilson
    100

    4.0.3 seems to have fixed this and you can now manually populate the properties in the constructor from the virtual Settings member, just remember to seal your class!

Please Sign in or register to post replies

Write your reply to:

Draft