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.
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.
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?
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.
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?
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:
You can subscribe to the event from an
ApplicationStarting
event: https://our.umbraco.org/documentation/reference/events/application-startupInside 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:
You should be able to adapt that for your purposes.
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?
The above is not working because in my EventHandler.cs it does not know the model. Any hints?
The
FormSubmissionContext
should have the ID of the page the form was submitted from. It may even have the fullIPublishedContent
instance (I forget). If it doesn't, I think it has anUmbracoHelper
you can use to get theIPublishedContent
instance. If not, you can create anUmbracoHelper
to do so.Poke around in the context object and see what you can find.
Yes, it works! Including Date formatting.
is working on a reply...