Copied to clipboard

Flag this post as spam?

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


  • lori ryan 239 posts 573 karma points
    May 05, 2021 @ 13:44
    lori ryan
    0

    Umbraco forms -max amt in numeric field

    Is there a way to limit the number of characters in numeric fields

  • lori ryan 239 posts 573 karma points
    May 05, 2021 @ 14:10
    lori ryan
    0

    came across this Overriding default providers in Umbraco Forms This is a new feature in Forms 6.0.3+ that makes it possible to override & inherit the original provider, be it a Field Type or Workflow etc. The only requirement when inheriting a fieldtype that you wish to override is to ensure you do not override/change the Id set for the provider, and make sure your class is public.

    Here is an example of overriding the Textarea field aka Long Answer that is taken from Per's CodeGarden 17 talk, which has been updated for Forms 8.

    public class TextareaWithCount : Umbraco.Forms.Core.Providers.FieldTypes.Textarea
    {
        // Added a new setting when we add our field to the form
        [Umbraco.Forms.Core.Attributes.Setting("Max length",
        Description = "Max length",
        View = "TextField")]
        public string MaxNumberOfChars { get; set; }
    
        public TextareaWithCount()
        {
            // Set a different view for this fieldtype
            this.FieldTypeViewName = "FieldType.TextareaWithCount.cshtml";
    
            // We can change the default name of 'Long answer' to something that suits us
            this.Name = "Long Answer with Limit";
        }
    
        public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context, IFormStorage formStorage)
        {
            var baseValidation = base.ValidateField(form, field, postedValues, context, formStorage);
            var value = postedValues.FirstOrDefault();
    
            if (value != null && value.ToString().Length < int.Parse(MaxNumberOfChars))
            {
                return baseValidation;
            }
    
            var custom = new List<string>();
            custom.AddRange(baseValidation);
            custom.Add("String is way way way too long!");
    
            return custom;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft