Yii 2 Dropdown List with Boolean Values

So that presented a challenge for me. I could either do what I was comfortable with, create a table to hold the values, and lose the boolean constraint, or learn the other way to do it, with virtually no help and no instruction.

I chose to tackle the latter because at the end of the day, I need to be able to deliver an enterprise solution capable of handling large queries. There’s no point in not learning it, other than laziness, and that isn’t going to help me become the ninja that I want to be. So really I had no choice but to tackle it head on.

So obviously I’ve seen examples of dropdown lists made from methods using constants in the past, as I’m sure everyone has at one point or another, so I started there. But then I remembered an example from a book I had read on Yii 1, and I remembered thinking how inefficient it was to have 3 methods just to service a dropdown list, not to mention the constants that get thrown at the top of the page. It just seemed so sloppy to me. And having worked with relationship methods, which are so clean, concise and powerful, I figured there had to be a better way.

So what I needed to do. Hand in 0 => “no”, 1 => “yes” into the dropdown list, and at the same time make sure that the form picked up the correct value to save when appropriate, and also make sure that I got the word displayed instead of the number where appropriate.

This is especially handy for situtations like when you are determining is_featured for example. You would expect the answer to that question to be a yes or a no, and that’s what should be displayed in a form or a view page for friendly UI.

Now as I’ve already stated, I was quick at doing this in a situation with a relationship and Yii 2 has some amazing tools to facilitate that. For example:

public static function getFaqCategoryList()
 $droptions = FaqCategory::find()->asArray()->all();
 return Arrayhelper::map($droptions, 'id', 'faq_category_name');

In the above code, Yii 2 gives you this great ArrayHelp::map method. You tell it which array, and what keys to map, and it returns values for them. It’s awesome. A quick call to the model fills up the $droptions array. I named it droptions as a morphed combination of “dropdown options,” in case anyone is wondering, but it’s just a local variable and you can call it anything you want. In this case I wanted the id as key and the category name as value.

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