How can I add an International Calling Code select input?

caught-in-a-nette
Member | 6
+
0
-

International calling codes have several duplicates (eg. USA = +1, Canada = +1), actually I believe all countries in North America use code 1.
PHP will not allow duplicate keys – however such an array can be stored if wrapped in another array. That doesn't help much. The values can be stored in a database though. Still processed by PHP…

As far I can see, the only way is to write the HTML select and options by hand. It would be nice to set dynamic classes and other attributes.

I believe the options should be like this:

<option value="1">USA (+1)</option>

Is there a way of making a form with “addSelect” and providing an array (or with “setItems”) that has duplicate codes?

Rick Strafy
Nette Blogger | 78
+
+1
-

Hi, I don't think so, there is useKeys parameter in setItems, but it looks like duplicated values will be still removed.

I suggest having values like 1-USA, and in processing of the form data you can do intval(explode('-', $data['code'])[0]) to read the code, that way you can avoid using manual rendering.

caught-in-a-nette
Member | 6
+
0
-

That's a good way to get around this issue. Thanks