⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Bug ! Not "selected" dropdown field using field_type



Đàm Khánh

Đàm Khánh
  • profile picture
  • Member

Posted 31 October 2013 - 07:53 AM

Hi ! I 'm not good at English.I'm using grocery crud to build project, but i have a problem when i user field_type with dropdown field .When i get value from database using dropdown with value=1 , dropdown field have "selected" but when value="0" , downdown have not "selected". My code : $this->grocery_crud->field_type('status','dropdown',array('0'=>'Old','1'=>'New'));. I dont know why ! please help me


alvinet

alvinet
  • profile picture
  • Member

Posted 20 January 2014 - 14:57 PM

I'm having the same problem.

I think it's like if 0 is reserved to "no value"


Jay Rebellion

Jay Rebellion
  • profile picture
  • Member

Posted 14 February 2014 - 15:55 PM

You're right, the get_dropdown_input function in the grocery_crud library does a 'is_empty' on the selected value. As 0 == empty in php, it thinks nothing is set.

 

I fixed this by editing around line 2337 in libraries/Grocery_CRUD.php:

$selected = !empty($value) && $value == $option_value ? "selected='selected'" : '';

change to:

$selected = !is_null($value) && $value == $option_value ? "selected='selected'" : '';

Which seemed to work.


michaelh613

michaelh613
  • profile picture
  • Member

Posted 07 November 2017 - 09:17 AM

Seems to be an old problem. It is still occurring today


fabianoc

fabianoc
  • profile picture
  • Member

Posted 28 February 2020 - 17:30 PM

2020, it still happens

Jay's solution works like a charm