Is it possible to have a conditional prevalue source, based on a user-submitted value?
I have a multi-page form for a scheduling service. One the first page, the user is asked to enter their postal-code, then on the second page they should see a dropdownlist with a list of dates (for the scheduling).
The dates will come from a 3rd-party web-service, that accepts a postal-code as the input - as the dates are relevant to the user's region/location.
So far I've been looking at using a custom FieldPreValueSourceType to handle this, (then querying the 'Partially Submitted' record to get the postal-code) ... but wasn't sure if there was a nicer/cleaner way to handle this?
When the user makes the first choice, you can make an AJAX call to the server for populating the dropdown, so when the user clicks "Save", the value from the dropdown is stored. I think this will give the best experience for the user.
I ended up using a Session variable to temporarily store the postal-code - by hooking into the RecordService.RecordPartiallySubmitted event.
public class AppStart : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);
RecordService.RecordPartiallySubmitted += RecordService_RecordPartiallySubmitted;
}
private void RecordService_RecordPartiallySubmitted(object sender, RecordEventArgs e)
{
var zipcode = e.Context.Request["ca5d8e93-244c-4fc4-ba34-ee9c4dcc9413"];
if (!string.IsNullOrWhiteSpace(zipcode))
e.Context.Session.Add("zipcode", zipcode);
}
}
(I used the Request object directly here - but I could have checked the e.Record.RecordFields to get the same value)
Then have a custom FieldPreValueSourceType to pick up the Session variable.
public class MyCustomPrevalueSourceType : FieldPreValueSourceType
{
// <snip>
public override List<PreValue> GetPreValues(Field field)
{
var zipcode = UmbracoContext.Current.HttpContext.Session["zipcode"].ToString();
if (!string.IsNullOrWhiteSpace(zipcode))
{
var dates = GetTheScheduledDatesFromTheWebService_GoGoGo(zipcode);
if (dates != null)
{
return dates
.Select((x, i) => new PreValue()
{
Field = field != null ? field.Id : Guid.Empty,
Id = x.Id,
SortOrder = i,
Value = x.Value
})
.ToList();
}
}
return new List<PreValue>();
}
// <snip>
}
Hope that helps if anyone else wants to do similar.
Contour - Conditional Prevalues
Is it possible to have a conditional prevalue source, based on a user-submitted value?
I have a multi-page form for a scheduling service. One the first page, the user is asked to enter their postal-code, then on the second page they should see a dropdownlist with a list of dates (for the scheduling).
The dates will come from a 3rd-party web-service, that accepts a postal-code as the input - as the dates are relevant to the user's region/location.
So far I've been looking at using a custom
FieldPreValueSourceType
to handle this, (then querying the 'Partially Submitted' record to get the postal-code) ... but wasn't sure if there was a nicer/cleaner way to handle this?Thanks,
- Lee
Comment author was deleted
Nothing in there by default that handles this so you'll need to do a bit of hacking
Thanks Tim. I'll get hacking! :-)
I'll post a code snippet later on.
When the user makes the first choice, you can make an AJAX call to the server for populating the dropdown, so when the user clicks "Save", the value from the dropdown is stored. I think this will give the best experience for the user.
I ended up using a Session variable to temporarily store the postal-code - by hooking into the
RecordService.RecordPartiallySubmitted
event.(I used the Request object directly here - but I could have checked the
e.Record.RecordFields
to get the same value)Then have a custom
FieldPreValueSourceType
to pick up the Session variable.Hope that helps if anyone else wants to do similar.
Cheers,
- Lee
Comment author was deleted
Nice, thanks for posting the example :)
is working on a reply...