Copied to clipboard

Flag this post as spam?

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


  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Oct 11, 2018 @ 08:34
    Owain Williams
    0

    How to add - Select - text in dropdown menu

    Hi,

    I have a dropdown menu on a form and I'd like to add the words Select option as the prevalue - so when the user first lands on the page, the menu has this value selected, I've managed to do this but the issue I have is that this is being registered as a selected value when I do a form submit. I need validation to still work e.g. Please select an option from the dropdown if Select Option is still selected.

    How do I go about doing this?

    O.

  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Oct 11, 2018 @ 08:54
    Owain Williams
    0

    One option which has come up is that I edit the dropdown code and hardcode the text. It would roll out over all dropdown menus but that's not a bad thing.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 11, 2018 @ 09:19
    Jan Skovgaard
    0

    Hi Owain

    I think that is probably the best and most consistent solution since I can't imagine a scenario where you would have it any different anyway? Sounds like a more clean approach rather than fiddling around with JavaScript and messing around with the DOM :)

    But it's a been a while since I did anything with forms myself so I have not tried theming yet - So don't know if a custom theme let's yout add this without touching some of the default files maybe?

    I'm just wondering about what will happen if an upgrade is released and you make a change in the default file - I suppose it will most likely be overwritten unless you remember to undo the overwrite of course. But if it's possible to avoid this by using themes then that might even be a better way to go. But as mentioned I don't know if it's possible to use a theme for dealing with this.

    Just my additional thoughts - Hope they're useful :)

    /Jan

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 11, 2018 @ 11:01
    Chriztian Steinmeier
    102

    Hi Owain,

    Using a custom Theme (or overwriting the default) you should be able to do it (unless that's ecxactly what you did and it doesn't behave as I speculate below).

    The default partial for the <select> is in ~/Views/Partials/Forms/Themes/default/Fieldtypes/FieldType.DropDownList.cshtml and just renders an empty <option value=""></option> as the first option. Keeping the value blank while adding your custom message inside should keep its behavior (99% sure, but easily tested).

    EDIT: Jan asks about upgrading - if you create a custom theme it will "survive" upgrades, and its possible to only copy that single file into the custom theme folder and UmbracoForms will just fallback to the default files for everything else.

    Hope that helps,

    /Chriztian

  • Owain Williams 481 posts 1413 karma points MVP 7x c-trib
    Oct 11, 2018 @ 11:03
    Owain Williams
    0

    Great thanks Chriztian!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 11, 2018 @ 11:16
    Jan Skovgaard
    0

    @Chriztian - My comment about that was not when theming was used but if theming was not used and more to highlight that the change in the core template file would be overwritten when upgrading - But I can see I did not formulate it very well. So thank you for clarifying :)

    /Jan

  • Colin Wiseman 52 posts 194 karma points
    Jan 31, 2019 @ 12:40
    Colin Wiseman
    0

    I had this same issue today, but it was to do with the theme having <option value=""></option> at the top of the dropdown list.

    But in the forms editor it has "Default Value" which I populated with "Select..." and changed

    <option value=""></option>
    

    to

     <option value="">@Model.Value</option>
    

    and it worked as I wanted it to, without hard coding values. It appears Model.Value is the default value but it would be good to have Model.DefaultValue instead in the API.

  • Liam Dilley 172 posts 402 karma points
    Jul 16, 2020 @ 00:41
    Liam Dilley
    0

    As an update on this.. This is what I do:

    I like to put answers for the next person that comes along from google.

    Either in your theme Fieldtypes overide folder or the default forms theme.. In: FieldType.DropDownList.cshtml

    You want to add:

    @{
    var settings = Model.AdditionalSettings;
    }
    

    And then you want to replace:

    <option value=""></option>
    

    With:

    <option value="">@settings["DefaultValue"]</option>
    

    Then you can use the default value field on the property in the admin to populate and the value still is 0.

Please Sign in or register to post replies

Write your reply to:

Draft