Copied to clipboard

Flag this post as spam?

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


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 06, 2013 @ 11:06
    Lee Kelleher
    0

    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

    Aug 06, 2013 @ 11:12

    Nothing in there by default that handles this so you'll need to do a bit of hacking

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 06, 2013 @ 11:19
    Lee Kelleher
    0

    Thanks Tim. I'll get hacking! :-)

    I'll post a code snippet later on.

  • Anders Bjerner 487 posts 2995 karma points MVP 8x admin c-trib
    Aug 06, 2013 @ 13:42
    Anders Bjerner
    1

    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.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 06, 2013 @ 16:41
    Lee Kelleher
    101

    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.

    Cheers,
    - Lee

  • Comment author was deleted

    Aug 06, 2013 @ 16:43

    Nice, thanks for posting the example :)

Please Sign in or register to post replies

Write your reply to:

Draft