Home › Forums › General Issues › Select default value
I’m struggling with displaying a default value from a select box.
I have a series of custom fields which are displayed in the User Profile page. One of them is a select box which contains a list of UK counties. I am trying to get it to work by starting the values with ‘- Select -‘. I did this by making the field required (which it has to be), entering value/label pairs e.g.
Aberdeenshire : Aberdeenshire
Anglesey : Anglesey
Angus (Forfarshire) : Angus (Forfarshire)
Antrim : Antrim
etc…
Leaving the default value empty, setting Allow Null to yes, multiple value to no, conditional logic to no. This doesn’t work. When I look at it on the users profile page, ‘Aberdeenshire’ is still the displayed value. I’ve also tried by putting ‘null’ in the default value but this doesn’t work either.
It doesn’t work the way you’re trying to use it.
The choices field does not allow you to enter a empty value.
When you set Allow null
to No
ACF does not add a - Select -
item so that a value is always selected. You can set the default if you don’t want the first one selected.
When you set Allow null
to Yes
ACF does add a - Select -
item so that the user does not need to select a value.
There have been some discussions in the past here about using an acf/load_field filter to alter the selections to include a null/empty value when a field is required, but I’ve been looking for it and could not find it. You could try that, see http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Hi John,
Thanks for your guidance 🙂
Just so I understand you correctly, here’s a screenshot of my settings when creating the Custom Field:
https://dl.dropboxusercontent.com/u/167786/acf-select-1.png
And here’s a screenshot of how it appears to the user:
https://dl.dropboxusercontent.com/u/167786/acf-select-1-user.png
As you can see, -Select- is added, but it still gets ignored in favour of the first actually entry (Aberdeenshire). If I switch to this:
https://dl.dropboxusercontent.com/u/167786/acf-select-2.png (with allow null now set to ‘No’. Then the user gets this with no -Select-
https://dl.dropboxusercontent.com/u/167786/acf-select-2-user.png
With the first case I inspected the select box element and saw this:
https://dl.dropboxusercontent.com/u/167786/acf-select-3-user.png
As you can see the -Select- option is ignored and ‘Aberdeenshire’ is selected like so: <option value="Aberdeenshire" selected="selected">Aberdeenshire</option>
I should also note I have tried both of those set ups above with a default value of null as here:
https://dl.dropboxusercontent.com/u/167786/acf-select-4.png
And also -Select – as here:
https://dl.dropboxusercontent.com/u/167786/acf-select-5.png
Is this what you’d expect to see based on your explanation above?
Thanks,
Kev
I’m not sure if this is a bug or not. To be honest, I’ve just used the ACF fields the way they work and haven’t really noticed this.
So I have another suggestion.
Create an choices list that looks something like
none : -- SELECT --
Aberdeenshire : Aberdeenshire
Anglesey : Anglesey
etc...
Set it allow null to no
set the default value as none
then create a custom validation filter that returns ‘Please Select a County’ if the value is ‘none’
Hi John,
Thanks again for your input 🙂
It looks like I might’ve solved the mystery!
Just on a hunch I created a brand new user and then logged in as them. It worked just fine. So the only reason it didn’t seem to be working was because it *was* acting correctly – e.g. an existing user had probably already chosen a county.
But thank you – at the very least because of your help I’ve discovered other things I can do with ACF!
Kev
The topic ‘Select default value’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.