Copied to clipboard

Flag this post as spam?

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


  • RunnicFusion 62 posts 145 karma points
    Jun 12, 2020 @ 08:28
    RunnicFusion
    0

    Umbraco Forms: hide conditional fields from email

    Hi!

    Inside my Umbraco Forms generated e-mails I would like to hide fields that are not displayed because of active conditions. For example: I want to show field 2 only if the condition is met and otherwise hide it in my e-mail. After a fit of coding i've found the .IsValid(form, string) method inside Umbraco.Forms.Core and start tweaking it.

    var dictionary = new Dictionary<Guid, string>();
    dictionary.Add(field.Id, string.Join(",", field.Values ?? new List<object>()));
    var showField = field.Condition.IsVisible(form, dictionary);
    

    And using it like this:

    parsedRecordFields = record
                        .RecordFields
                        .Values
                        ?.Where(n => !n.Field.HasCondition() || FieldIsInsideSelectedCondition(e.Form, n.Field))
    

    But that isn't working as expected; all return values (showField) are false, even if they need to be true.

    The FieldConditionEvaluation.IsVisible is expecting the Form and an dictionary (GUID, string) as parameter and can be found inside the namespace Umbraco.Forms.Core.

  • MagnusWL 1 post 21 karma points
    Dec 09, 2020 @ 08:34
    MagnusWL
    0

    Hey,

    I've run into a similar issue where i want to display only the visible fields on a summary page. FieldConditionEvaluation.isVisible() is always false, there is also a method FieldConditionEvaluation.Test() which test the conditions but it doesn't take into consideration the visibility of the fields which the rules depend on, which Forms does. Eg if Field1 gets set to "Value1" and then the user goes back and changes input so Field1 is not not visible, then the Test() method will not take into consideration that this field is no longer visible.

    The TestRule() has the same issue, but I have made my own method to take thsi into account. It requires that you make a dictionary 'allFields' with all the FieldViewModels, which i get from iterating through the FormViewModel gotten from the TempData.

    Dictionary<string, FieldViewModel> allFields = new Dictionary<string, FieldViewModel>();
    

    The method recursively tests the fields with the TestRule() method.

                                            bool TestField(Umbraco.Forms.Mvc.Models.FieldViewModel fieldParam)
                                            {
                                                if (fieldParam.Condition.Enabled)
                                                {
                                                    bool show = fieldParam.Condition.ActionType == 0;
                                                    bool all = fieldParam.Condition.LogicType == 0;
                                                    bool allrulesmet = true;
                                                    bool anyrulesmet = false;
    
                                                    foreach (var rule in fieldParam.Condition.Rules)
                                                    {
                                                        if (Umbraco.Forms.Core.FieldConditionEvaluation.TestRule(rule, nameValueDictionary)
                                                            && TestField(allFields[rule.Field.ToString()]))
                                                        {
                                                            anyrulesmet = true;
                                                        }
                                                        else
                                                        {
                                                            allrulesmet = false;
                                                        }
                                                    }
    
                                                    if (all && allrulesmet)
                                                    {
                                                        return show;
                                                    }
    
                                                    if (!all && anyrulesmet)
                                                    {
                                                        return show;
                                                    }
    
                                                    return !show;
                                                }
                                                return true;
                                            }
    
                                            bool display = TestField(field);
    

    Using Umbraco Forms 8.1.4

Please Sign in or register to post replies

Write your reply to:

Draft