Copied to clipboard

Flag this post as spam?

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


  • Tommel 6 posts 106 karma points
    Feb 28, 2018 @ 14:16
    Tommel
    0

    Add value from page to form

    Hi,

    I have a event detail page with the event description, date etc. People can subscribe to this event using a Formulate form but i would like to include the date of the event in the email/storage of data. How can i add a field to the form dynamically or static using a hidden field with the desired value.

    I have looked at: ("Dynamic Email Parameters"): https://github.com/rhythmagency/formulate/releases/tag/v1.2.1 But i don't get it where to place that code (I am not a die-hard programmer ;-) )

    Any ideas/help?

  • Nicholas Westby 1883 posts 6259 karma points c-trib
    Mar 05, 2018 @ 17:36
    Nicholas Westby
    0

    You are going down the recommended path, so it sounds like you just need a couple tips of how to go about it.

    The code you've found shows how you can modify a submission before it gets stored:

    // Subscribe to form submitting event.
    Submissions.Submitting += Form_Submitting;
    private void Form_Submitting(FormSubmissionContext context)
    {
        // Modifications here.
    }
    

    You can subscribe to the event from an ApplicationStarting event: https://our.umbraco.org/documentation/reference/events/application-startup

    Inside of that, you can modify the values of the form submission. If you make one of your fields a hidden field, you can modify its value. Note that you can't "add" a field dynamically, but so long as the field is already there to start with, you can change its value.

    Here's some code that's doing something similar:

    /// <summary>
    /// Sets the success indicator field to "Unknown".
    /// </summary>
    /// <param name="context">
    /// The form submission context.
    /// </param>
    private static void SetSuccessIndicatorField(FormSubmissionContext context)
    {
    
        // Find the success indicator field (exit early if not found).
        var field = context.Form.Fields
            .FirstOrDefault(x => x.Category == "Success Indicator");
        if (field == null)
        {
            return;
        }
    
        // Get or create the field data.
        var newData = new List<FieldSubmission>(context.Data);
        var fieldData = newData.FirstOrDefault(x => x.FieldId == field.Id);
        if (fieldData == null)
        {
            fieldData = new FieldSubmission()
            {
                FieldId = field.Id
            };
            newData.Add(fieldData);
            context.Data = newData;
    
        }
    
        // Set field value to "Unknown".
        fieldData.FieldValues = new List<string>() { "Unknown" };
    
    }
    

    You should be able to adapt that for your purposes.

  • Tommel 6 posts 106 karma points
    Mar 06, 2018 @ 11:18
    Tommel
    0

    Hi Nicholas,

    Thank you for your reply and great package! I managed to change the value of a certain field to a static value by using the code above. But how can i change it to a dynamic value out of the model?

    // Set field value to Evendate.
                fieldData.FieldValues = new List<string>() { Model.Content.GetPropertyValue<DateTime>("DateTimeEvent") };
    

    The above is not working because in my EventHandler.cs it does not know the model. Any hints?

  • Nicholas Westby 1883 posts 6259 karma points c-trib
    Mar 06, 2018 @ 16:26
    Nicholas Westby
    0

    The FormSubmissionContext should have the ID of the page the form was submitted from. It may even have the full IPublishedContent instance (I forget). If it doesn't, I think it has an UmbracoHelper you can use to get the IPublishedContent instance. If not, you can create an UmbracoHelper to do so.

    Poke around in the context object and see what you can find.

  • Tommel 6 posts 106 karma points
    Mar 07, 2018 @ 09:50
    Tommel
    101

    Yes, it works! Including Date formatting.

    fieldData.FieldValues = new List<string>() { umbraco.library.FormatDateTime(context.CurrentPage.GetProperty("DateTimeEvent").Value.ToString(), "dd MMMM yyyy") };
    
Please Sign in or register to post replies

Write your reply to:

Draft