Support Forum

Welcome to the Support forum.

New Discussion can be made between Monday and Friday (AEST). Comments are never disabled.

Thanks, Elliot

Current status: Solved. . Done
Select field - Default Value - Not working?
  • This could be just me not understanding the feature, but here is the situation:

    I have a repeater field with 4 sub-fields:

    Low Price - Number
    High Price - Number
    Price Range - Select
    Call for Pricing - Checkbox

    I need the Price Range - Select box to be in the order:
    priceone : $15,000 and under
    pricetwo : $15,001 - $25,000
    pricethree : $25,001 - $40,000
    pricefour : $40,000 and up

    But I need the default to be pricefour : $40,000 and up

    I have tried setting the default value field to:
    pricefour
    $40,000 and up
    pricefour : $40,000 and up

    but it always defaults to: $15,000 and under when attempting to add a new custom post type item.

    The documentation here on ACF doesn't specify the expected format for the default value and a search of other open tickets didn't turn anything up.

    If I could, I would simply change the order of the dropdown menu, but that's not an option with this client...

    Thanks!


  • Ok. I should probably have done this before posting my first comment...(I hate posting without giving a possible solution as well... ;) )

    I took a look at the code and the default_value doesn't appear to be used at all.

    Version 3.5 - core/fields/select.php - Line 124 (Edit: tabs removed from code for readability.)
    was:
    if($key == $field['value'])


    I changed it to:
    if($key == $field['value'] || ('' == $field['value'] && $key == $field['default_value']))


    I didn't see anywhere else in there that this would need to be modified, however, I'm not quite sure what the OPTGROUP section is for, since I don't see where that can be set anywhere... :) May need to look at adding something for the default value there in an ELSE statement for if($value) in the OPTGROUP area.

    But for now, I hope that my suggestion, or something like it that keeps functionality, will be added in the next release. I need this for my project (and it seems to be a bug to me. ;) )

    Thanks again. If I haven't said it recently, I really love ACF and appreciate your hard work keeping it moving forward!
  • I was trying to create new posts. I understand that the value wouldn't be changed for existing posts.

    It doesn't work without the code I provided. There aren't any "errors" per se. It just doesn't set the default value.

    You may want to note that in my IF statement, field['value'] is checked to be equal to '', which to me appears that the value is not being set before this point as well.

    It does not set a default value. And ignore anything I said about OPTGROUP for the purpose of my specific problem, because I have no idea what that code does and I'm not really concerned with it, I only mentioned it because you may be concerned with it if it isn't working as expected along with the standard select code.
  • Hi @ancyfiber

    ACF will set the $value variable to the default value on a "new" post. If you edit an existing post, the default value will not be loaded.

    The code does not need to change as the default value would have been loaded well before this if statement.

    Please try creating a new post and report back with any errors.

    Thanks
    Elliot
  • @admin

    Not wanting to be a pest, but I just wanted to check if you had time to review this. I checked the to-do list and didn't see any reference to this or anything that looked similar.

    Let me know if I need to be more specific about the issue, or why I put the code I did in there.

    Much appreciated.
  • Hi @fancyfiber

    I can see your code, but I do believe the default value is already working fine.

    I'll do some testing and get back to you.

    Cheers
    Elliot
  • Hi @admin

    Again, not trying to be a pest. Just checking in on the status of this. I checked the (updated) to-do-list and this still doesn't show up as something to be fixed. I realize you are busy with many things and I am not trying to pressure you. I just don't want this to fall off the radar because you believe it is working.

    I realize you think I'm an idiot (I'm really trying not to take offense, when I'm sure it wasn't intended, but I'm failing) for questioning your code. I am not trying to be rude in questioning things by any means though. I really like ACF and use it on many projects that I work on and simply am trying to help you improve the product over all.

    When you say that $value is set, this is a true statement, if you look at the code, $value goes hand-in-hand with $key. $value is not the default value. It does not associate with anything. When looping through to display the selected value, based on the $key, a new post doesn't have any $field['value'] set. You see my code, so you know what it is doing, which is very basic.

    After looking at the code again, it probably would make more sense to add something like this after the $field = array_merge($defaults, $field). This will simply allow the value to flow through like normal during the looping process.
    //Set the default field value if the value does not currently exist and a value is set for the default
    if($field['value'] == '' && $field['default_value'] != '') $field['value'] = $field['default_value'];


    Currently in the code there is nothing being done with the $field['default_value'] setting to set it anywhere in a new post, which is why I am stating this is a bug. I want to stress, I am not trying to simply point out flaws, but I am trying to help improve an already wonderful product that I enjoy using as well as make my own life simpler going forward so that I don't have to keep updating ACF code when new versions are released because I need them to work.

    Thanks for your time.
  • Hi @fancyfiber

    Yes, the select field does use the value key to compare.

    However, before the select runs its code to display the html, ACF changes the value of value to the value of default_value.

    This is only if you are on a new post. Not editing an old one

    Does this help to explain it?
  • Hi @admin

    Yes and in many (probably most) cases that appears to work. I didn't follow the code far enough back because it does not work in my specific instance and the code I put in fixes my issue.

    Test this, please: Using ACF 3.5.3.1

    Create a repeater field with the minimum rows set to 1. (In my further testing max rows didn't seem to matter.)

    Then create a select field in the repeater field. With a default value.

    I'm really not crazy! :)
  • Hi @admin

    I have retested this with version 3.5.5 and default values with a repeater field with a minimum rows set to 1 doesn't set the default value.

    Any chance you can look into this sometime?

    Thanks!
  • Hi @fancyfiber,

    Thanks mate, I've fixed this issue and uploaded it to the nightly build on github

    Cheers
    Elliot