Yii 2 Dropdown List with Boolean Values

So obviously the reason I couldn’t just use this code for my is_featured boolean is that the values do not exist in the DB, so I don’t need to return them as an array. Now here’s where I went astray. I figured I should still use the ArrayHelper and just create the array manually. And I almost went for that.

But then, since I don’t work with straight arrays that often, I started messing up the syntax. It was actually quite comical as I failed one attempt after another to manipulate an associative array with two values in it. But such is the nature of programming. When we go down the wrong road, we tend to go a long ways down that road.

So why am I bothering to tell you all this? Because I want you to understand why I’m documenting as much as I can about Yii 2. I want you to be able to gain as much insight and knowledge about the framework as possible as quickly as possible. I spent two hours figuring this out, so you don’t have to. Hopefully you will pass these tips on to others…

Anyway, back to the solution. After wrestling with it for a while, I realized I was making it more difficult than it needed to be. I really only need two methods, 1 to return the dropdown list key value pairs and the other to return the names for display.

So, drum roll please…. Here you go:


public static function getFaqIsFeaturedList()
{
            
 return $droptions = [0 => "no", 1 => "yes"];
       
        
}
    
public function getFaqIsFeaturedName()
{
            
return $this->is_featured == 0 ?  "no" : "yes";
        
      
}

Now we’re getting it! Very clean and concise. If you need to populate a dropdown list with a boolean, create a method like the above on your model. Then use the following line in your form view:


<?= $form->field($model, 'is_featured')->dropDownList($model->faqIsFeaturedList, [ 'prompt' => 'Please Choose One' ]);?>

In this case, we are using a dropdown list with the values of Yes and No, with a prompt of Please Choose one. The field we are filling is is_featured, which as we have said, is a boolean. Note the use of the lowercase f on $model->faqIsFeaturedList. That is because we are using Yii 2’s magic get method to reference the method. Alternatively, we could have used:


<?= $form->field($model, 'is_featured')->dropDownList(Faq::getFaqIsFeaturedList(), [ 'prompt' => 'Please Choose One' ]);?>

In this case we are using at method from the Faq model and getFaqIsFeaturedList is a public static method and can be called that way.

Advertisements

2 thoughts on “Yii 2 Dropdown List with Boolean Values”

  1. Is it possible to create a dropdownlist with a boolean!?
    I’m suffering!

    field($model, ‘bln_reprogramacao’)->DropDownList([1 => ‘Yes’,0 => ‘no’],[‘prompt’=>”])?>

    How can I make it work??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s