Copied to clipboard

Flag this post as spam?

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


  • Imran 17 posts 115 karma points c-trib
    Nov 20, 2018 @ 12:29
    Imran
    0

    Umbraco Forms custom field type custom settings property not saving

    I have created a new custom field type and within that a settings property.

        [Umbraco.Forms.Core.Attributes.Setting("Test Property", description = "Test Description")]
        public string TestProperty { get; set; }
    

    For some reason it is not saving when I submit. I can debug the variable and see that the value is being set, but when I navigate away or refresh then the value is gone.

    Any ideas? :)

  • ianhoughton 281 posts 605 karma points c-trib
    Nov 20, 2018 @ 13:52
    ianhoughton
    0

    This is how we're doing our custom field types, can you show the rest of the FieldType code?

    public class CareCategoryField : FieldType
    {
        [Required(ErrorMessage = ValidationRequiredCareCategory)]
        [Setting("Care category", view = "TextField")]
        public string DefaultValue { get; set; }
    
        public CareCategoryField()
        {
            Id = new Guid(FieldTypeId.CareCategoryField.Description());
            Name = "Care category";
            FieldTypeViewName = "FieldType.CareCategory.cshtml";
            Description = "Used to capture the mandatory care code";
            Icon = "icon-categories";
            DataType = FieldDataType.String;
            Category = "Simple";
            SortOrder = 10;
        }
        public override string GetDesignView()
        {
            return "/App_Plugins/UmbracoForms/Backoffice/Common/FieldTypes/hiddenfield.html";
        }
    
        public override bool HideLabel
        {
            get { return true; }
        }
    
        public override bool StoresData
        {
            get { return true; }
        }
    }
    
  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Mar 10, 2020 @ 16:19
    Markus Johansson
    0

    @ianhoughton Did you ever get the validation-attribute to work? When I've added it no validation is performed.

    Refering to this:

    [Required(ErrorMessage = ValidationRequiredCareCategory)]
    
  • Imran 17 posts 115 karma points c-trib
    Nov 20, 2018 @ 14:37
    Imran
    0

    I tried to use your settings and it still doesn't save. Here is my code:

    public class NewTestField : FieldType
    {
        [Setting("Test Setting", view = "TextField")]
        public string TestSetting { get; set; }
    
        public NewTestField()
        {
            this.Id = new Guid(FieldTypeId.GetNewTestFieldId());
            this.Name = "AgreementsField";
            this.Description = "Render a custom text field.";
            this.Icon = "icon-autofill";
            this.DataType = FieldDataType.String;
            this.SortOrder = 10;
            this.SupportsRegex = true;
        }
    
        public override string GetDesignView()
        {
            return "/App_Plugins/UmbracoForms/Backoffice/Common/FieldTypes/hiddenfield.html";
        }
    
        public override bool HideLabel
        {
            get { return true; }
        }
    
        public override bool StoresData
        {
            get { return true; }
        }
    
        // You can do custom validation in here which will occur when the form is submitted.
        // Any strings returned will cause the submit to be invalid!
        // Where as returning an empty ienumerable of strings will say that it's okay.
        public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context)
        {
            var returnStrings = new List<string>();
    
            if (!postedValues.Any(value => value.ToString().ToLower().Contains("custom")))
            {
                returnStrings.Add("You need to include 'custom' in the field!");
            }
    
            // Also validate it against the original default method.
            returnStrings.AddRange(base.ValidateField(form, field, postedValues, context));
    
            return returnStrings;
        }
    }
    

    What version of forms does this work on for you?

Please Sign in or register to post replies

Write your reply to:

Draft