Same Array for Nette\Forms::addSelect() as for Nette\Forms::addRadioList()

5 years ago

ic
Member | 413
+
0
-

If I'm using:

$countries = array(
    'Europe' => array(
        'CZ' => 'Czech republic',
        'SK' => 'Slovakia',
        'GB' => 'United Kingdom',
    ),
    'CA' => 'Canada',
    'US' => 'USA',
    '?'  => 'other',
);
$form->addSelect('country', 'Country:', $countries);

it works fine, but if I use same Array for addRadioList method $form->addRadioList('country', 'Country:', $countries); it creates somethink like this:

<input type="radio" name="country" id="frmsomeForm-country-0" value="Europe">
<label for="frmshippingForm-country-0">Array</label>
<br>
<input type="radio" name="country" id="frmsomeForm-country-1" value="CA">
<label for="frmshippingForm-country-1">Canada</label>
<br>
<input type="radio" name="country" id="frmsomeForm-country-2" value="US">
<label for="frmshippingForm-country-2">USA</label>
<br>
<input type="radio" name="country" id="frmsomeForm-country-3" value="?">
<label for="frmshippingForm-country-3">other</label>

That's not exactly what I was expected.
What about to ignore first nested Array and use only its items? like this:

$countries = array(
//    'Europe' => array(
        'CZ' => 'Czech republic',
        'SK' => 'Slovakia',
        'GB' => 'United Kingdom',
//    ),
    'CA' => 'Canada',
    'US' => 'USA',
    '?'  => 'other',
);
$form->addRadioList('country', 'Country:', $countries);

but automaticly XD

5 years ago

Quinix
Member | 108
+
0
-

I don't think this should work automatically, but you can do it yourself by using Arrays::flatten – https://api.nette.org/…ays.php.html#…

5 years ago

ic
Member | 413
+
0
-

Quinix wrote:

I don't think this should work automatically, but you can do it yourself by using Arrays::flatten – https://api.nette.org/…ays.php.html#…

but flatten() make something like this:

$countries = array(
    '0' => 'Czech republic',
    '1' => 'Slovakia',
    '2' => 'United Kingdom',
    '3' => 'Canada',
    '4' => 'USA',
    '5' => 'other',
);
$form->addRadioList('country', 'Country:', $countries);

It loosing my good old keys

Last edited by ic (2013-10-30 15:29)

5 years ago

Majkl578
Moderator | 1379
+
0
-

ic wrote:

It loosing my good old keys

I think it'd be good idea to have an option to preserve keys via 2nd parameter. :)

5 years ago

ic
Member | 413
+
0
-

Majkl578 wrote:

I think it'd be good idea to have an option to preserve keys via 2nd parameter. :)

sounds good