Copied to clipboard

Flag this post as spam?

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


  • Alessandro 30 posts 151 karma points
    Jun 09, 2022 @ 10:16
    Alessandro
    0

    Hi everybody, I need to set two different data consent fields that needs two show up based on conditional logic on the value of a dropdown.

    So if dropdown is value1 show dataConsent1, otherwise show dataConsent2. So far so good.

    The problem is that Umbraco Forms wants them to be both checked, also if one is not showing. This appears to happen only with Data Consent field, I've tried with other mandatory fields and there is not any problem.

    I've overriden the default DataConsent cshtml but I don't see any major difference.

        @model Umbraco.Forms.Mvc.Models.FieldViewModel
    @{
           var acceptCopy = string.Empty;
           Model.AdditionalSettings.TryGetValue("AcceptCopy", out acceptCopy);
    }
    <div class="d-flex">
        <input class="custom-check-input"
               type="checkbox" name="@Model.Id" id="@Model.Id" value="true" data-umb="@Model.Id" 
               @if(Model.Mandatory){<text> data-val="true" data-val-required="@Model.RequiredErrorMessage" data-rule-required="true" data-msg-required="@Model.RequiredErrorMessage"</text>}
               @if (Model.ContainsValue(true) || Model.ContainsValue("true") || Model.ContainsValue("on"))
               {<text>checked="checked"</text>}       
        />
        <label for="@Model.Id"></label>
        <p class="form-check-label">
            @Html.Raw(acceptCopy)
        </p>
        <input type="hidden" name="@Model.Id" value="false" />
    </div>
    

    Any clue? thanks

  • Alessandro 30 posts 151 karma points
    Jun 13, 2022 @ 09:51
    Alessandro
    100

    Solved:

        public class DataConsentFixed : Umbraco.Forms.Core.Providers.FieldTypes.DataConsent
    {
        private static readonly string s_trueString = true.ToString().ToLowerInvariant();
    
        public override IEnumerable<string> ValidateField(Form form, Field field, IEnumerable<object> postedValues, HttpContextBase context, IFormStorage formStorage)
        {
            Dictionary<Guid, string> fieldValues = form.AllFields.ToDictionary((Field f) => f.Id, (Field f) => string.Join(", ", f.Values ?? new List<object>()));
    
            List<string> list = new List<string>();
            if (field.Mandatory && !postedValues.Any((object x) => string.Equals(x.ToString(), s_trueString, StringComparison.InvariantCultureIgnoreCase)))
            {
                bool flag = true;
                if (field.HasCondition())
                {
                    flag = !field.Condition.IsCircular(form) && field.Condition.IsVisible(form, fieldValues);
                }
    
                if(flag)
                {
                    string item = ((!string.IsNullOrWhiteSpace(field.RequiredErrorMessage)) ? field.RequiredErrorMessage : "Consent is required to store and process the data in this form.");
                    list.Add(item);
                }
            }
    
            return list;
        }
    }
    

    In the Validate Field method of the DataConsent field type the visibility of the field was not taken into account. Overriding the method with some code taken from the FieldType.cs did the trick.

Please Sign in or register to post replies

Write your reply to:

Draft