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.
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.
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 :)
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.
@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 :)
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.
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
ifSelect Option
is still selected.How do I go about doing this?
O.
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.
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
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
Great thanks Chriztian!
@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
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
to
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 haveModel.DefaultValue
instead in the API.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:
And then you want to replace:
With:
Then you can use the default value field on the property in the admin to populate and the value still is 0.
is working on a reply...