Copied to clipboard

Flag this post as spam?

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


  • Greg Berlin 818 posts 634 karma points
    Sep 22, 2010 @ 04:56
    Greg Berlin
    0

    Best way to create a lookup field

    Hey all,

    I need to create a lookup field on a form for a client.  What i'd like to do is first load a page with a single text entry field where the user enters a town name. I then want to check that name against a database table, and if it exists forward the user onto step 2 of the form where they can complete the rest, if not then display a message saying their area is not supported.

    I'd like to do this all within contour, instead of creating the first step as a separate form (which would be easy via a user control, but not my ideal choice as i have contour to leverage, so why not i say.

    My questions:

    1. what would be the best way to achieve this?  I'd like to make it generic enough to reuse any new control types i create (perhaps by entering a table name and field name to a config).

    2. are there any examples available of this being done before?

    3. Best way to make the data in the lookup table viewable and editable by the client. I'd like to keep this within the contour section too - is it hard to add a new node to the contour tree, either a new node or under the current Data Sources node?

    4. how can i reference the value entered in step 1 in both step 2 of the form (to prepopulate a field), and in the email sent to the customer (this would be achieved already if the form is prepolated)?

    Thanks heaps

    Greg

     

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Sep 24, 2010 @ 12:36
    Tim
    0

    Hi Greg,

    I think the best way to do this would be to create a custom field type that does the lookup, although you'd probably have to do the lookup with AJAX, or roll in a custom validator into your field so that you can set Page.IsValid = false if the area isn't found (if you use a custom validator you'll need to set the validationgroup for it, or it'll be ignored. There's some talk about how to do that here: http://our.umbraco.org/forum/umbraco-pro/contour/12731-Default-ValidationGroup-name).

    As for creating a custom field, there's some good stuff in the documentation about it: http://umbraco.org/products/umbraco-contour/help-and-support/developer-documentation and there's also the handy open source field type/workflow package in the package library that contains the code for how all the default ones work, which can be downloaded here: http://our.umbraco.org/projects/umbraco-contour-shared-source

    As for editing the data, I'd maybe look at either adding a separate section in umbraco for it rather than having it as part of contour (although there's nothing stopping you from adding it to the contour tree that I can think of), it should just be the same as adding an item to any of the other trees in the CMS.

    for 4) I think you'd be looking at maybe another custom field type and the email could either be done with the default contour email builder (as long as the entered value is saved in the form, it can be added to an email) or via a custom workflow if you need more customisation.

    Hope that's useful!

    :)

  • Greg Berlin 818 posts 634 karma points
    Sep 24, 2010 @ 13:31
    Greg Berlin
    0

    Hey Tim,

    Thanks heaps for the info.  I have had a read through the documentation (found it after i posted the above), and thought of this as an approach:  I create a custom field type, which is bound to a data source as per a normal field type.  The custom type will accept a parameter of "Validation Failure Value", which i give a value such as -1.  Then when i create the field type, i'll bind it to the list of available towns, and add a "Not Found" value, with a value of -1.  The field type's validator only checks to ensure that this value is not selected.  If it is, then an error message displays (I thought i'd make the error message a parameter too, so i could enter some custome html to display on failure)

    This seems like a pretty neat way to do it, without having to do too much development (its for a charity so i'm trying to do it on the cheap as much as possible). The one thing i'm not entirely sure about is how the validation works... I assume if validation fails, the next button will be disabled?  If this is the case, then i can put the rest of the form in step 2 of the form, and step 1 has only the town lookup.

    I'm going to give this a go tomorrow, so i'll let you know how i progress.. let me know if you see anything wrong with my proposed approach (and perhaps save me chasing my tail unnecessarily) :)

    Cheers

    Greg

     

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Sep 24, 2010 @ 13:49
    Tim
    0

    I think that sounds ok! Let me know how you get on.

    :)

  • Greg Berlin 818 posts 634 karma points
    Sep 25, 2010 @ 16:28
    Greg Berlin
    0

    Ugh... this has been really really tedious to do.  Not enough working examples out there means i've had to fumble my way through it... 

    So far i've done the following:

    1. Created a custom FieldType that retrieves values from the form editor
    2. Created a custom web control (ddl with validaiton) that adds an unselected value, as specified in the FieldType settings
    I'm stuck on the validation of the custom web control. Its just not firing.  I posted here requesting info if anybody knows how to solve it: http://our.umbraco.org/forum/umbraco-pro/contour/12731-Default-ValidationGroup-name?p=0#comment47983
    It could be  because i'm not assigning my control to the right validation group, but i can't find which one i should assign it too.  There could be more to it than that, so i'd love to see a working sample of something similar being achieved.
    Thanks all
    Greg

  • Greg Berlin 818 posts 634 karma points
    Sep 26, 2010 @ 11:35
    Greg Berlin
    0

    Fixed it.. my solution is here: http://our.umbraco.org/forum/umbraco-pro/contour/12731-Default-ValidationGroup-name?p=0#comment48011

    So yeah all is looking good... i have my custom field type, custom web control, validation firing perfectly.  If validation successful, we can go to the rest of the form, if not display error message with some information for the user.

    Now to build the rest of the form, and refer to the value selected in this step, which i think should be fairly straight forward.  :)  

    I may have some issues displaying HTML info from my web control as the error message (instead of just flat text)... I'll keep you posted on that one

  • Greg Berlin 818 posts 634 karma points
    Sep 26, 2010 @ 13:02
    Greg Berlin
    0

    Yup, as i suspected i'm having trouble displaying html error messages.

    It turns out Contour is stripping all HTML values i enter into the field type settings.  I'll work around this for now by using a string with placeholders, and do some funky replacement in the web control.. its ugly but it will work.

    For future, any chance of getting the ability to pass html through to our custom field types? I've tried HtmlEncoding what i enter there, but Contour still strips it.

  • Greg Berlin 818 posts 634 karma points
    Sep 26, 2010 @ 13:51
    Greg Berlin
    0

    Phew, almost there... last thing i need to sort out is the town name.  It seems using the bracket syntax  {yourtown} returns the ID of the selected value, not the Text value itself.

    This is strange as the value displayed in the collected data shows the town name, not the ID.  Is there a trick to get the name instead of the ID?

Please Sign in or register to post replies

Write your reply to:

Draft