Forums

Default Selected Values for multi select input elements

bilal.abdeen 31 Mar, 2016
Problem#1: I am having difficulty setting the default values for input elements with multi select capabilities, i.e. checkbox and dropdown. I tried using the "Selected Values" field, which is on the element's General tab. However, it seems that only a single element can be checked. See the attached screen shot. What am I missing?

Problem#2: By the way, my objective is actually to get options selected based on data recorded in a database. In other words, the form will read the data, change the string into an array. I was successful in getting this far. Then, use the array to show some options as selected. When I struggled with doing that, I thought of trying (first) to hard-code the selected options (my question above.) By the way, I managed to read data from a database, and use the values to populate single-value input elements. However, I could not make it work for input elements with multi select capabilities, i.e. checkbox and dropdown.
GreyHead 31 Mar, 2016
Hi bilal.abdeen,

It looks as though there is a bug with setting multiple default values in a drop-down :-(

I dug around in the code and I think that the problem is that the data shows up as an 'array' that is broken. The array looks like
array([0] => car [1] => bus)
- there needs to be a comma after car
array([0] => car, [1] => bus)


I was able to set values using a Custom Code element:
<?php
$form->data['dropdown1'] = array('car', 'bus');
?>


it should be straight forward to set the default values from the database in a similar way - what do you see if you add a Debugger action?

Bob
bilal.abdeen 31 Mar, 2016
Bob,

Thank you for your effort and time. I really appreciate it.

It does'n to work, yet. I have added the following custom code just before HTML (render form.)

<?php
$form->data['transportation_method_used_to_come_to_iw'] = array('car', 'bus');
// This is what you suggested. Isn't it?
$form->data['spoken_languages[]'] = array('Arabic', 'Akan');
// I thought we need brackets for multi-select elements
$form->data['available_to_volunteer_on'][] = array('Friday' , 'Saturday'); 
// I have read somewhere a suggestion to put the brackets outside!
?>

Following is the debugging output. (...) denotes removed lines, because there are many fields in the form, which should be irrelevant to the problem here. (//) denotes comments, which were added by me.

Data Array
Array
(
    [chronoform] => RegForm
    [event] => submit
    [firstname] => Bilal
    [lastname] => Abdeen
....
    [spoken_languages] => Array    // This is a dropdown element, which has many choices. I selected 2
        (
            [0] => Arabic
            [1] => Akan
        )

....
    [available_to_volunteer_on] => Array        // This is a checkbox element, which has many choices. I selected 2
        (
            [0] => Monday
            [1] => Tuesday
        )

    [volunteer_area] => Array        // This is a checkbox element, which has many choices. I selected only 1
        (
            [0] => Other
        )

    [transportation_method_used_to_come_to_iw] => Array        // This is a checkbox element
        (
            [0] => Car
            [1] => Bus
        )

    [button177] => Register
    [fullname] => Bilal Abdeen
    [name] => Bilal Abdeen
....
    [_PLUGINS_] => Array
        (
            [joomla_registration] => Array
                (
                    [*isRoot] => 
                    [id] => 157
                    [name] => Bilal Abdeen
....
                    [block] => 
                    [sendEmail] => 0
                    [registerDate] => 2016-03-31 13:40:12
                    [lastvisitDate] => 
                    [activation] => 
                    [groups] => Array
                        (
                            [0] => 15
                        )

                    [lastResetTime] => 
                    [resetCount] => 
                    [requireReset] => 
                    [*_params] => Joomla\Registry\Registry Object
                        (
                            [data:protected] => stdClass Object
                                (
                                )

                            [separator] => .
                        )

                    [*_authGroups] => Array
                        (
                            [0] => 1
                            [1] => 13
                        )

                    [*_authLevels] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => 5
                        )

                    [*_authActions] => 
                    [*_errorMsg] => 
                    [*userHelper] => JUserWrapperHelper Object
                        (
                        )

                    [*_errors] => Array
                        (
                        )

                    [aid] => 0
                    [cookieLogin] => 
                    [chronoform] => RegForm
                    [event] => submit
                    [firstname] => Bilal
                    [lastname] => Abdeen
....
                    [spoken_languages] => Array
                        (
                            [0] => Arabic
                            [1] => Akan
                        )

                    [available_to_volunteer_on] => Array
                        (
                            [0] => Monday
                            [1] => Tuesday
                        )

                    [volunteer_area] => Array
                        (
                            [0] => Other
                        )
....
                    [transportation_method_used_to_come_to_iw] => Array
                        (
                            [0] => Car
                            [1] => Bus
                        )

                    [button177] => Register
                    [fullname] => Bilal Abdeen
                    [password2] => 1
                    [usertype] => deprecated
                )

        )

)


Thank you so much,
Bilal
GreyHead 01 Apr, 2016
Hi bilal.abdeen,

The options have two parts value=text

The value is returned when the form is submitted, the text is displayed in the dropdown.

You need to set the default *values*.

If your options are like
0=Arabic
1=Akan
then the values are 0 and 1

I suggest that you don't use these as they are unhelpful, instead use
Arabic=Arabic
Akan=Akan
or
arabic=Arabic
akan=Akan

I prefer to use lower case for the values because it helps me see the difference and avoids typos in my code.

The correct format for setting the values is the one I used without the extra square brackets - the array() tells PHP that this is an array of values. Using the lower case values:
<?php
$form->data['spoken_languages'] = array('arabic', 'akan'(;
?>

Bob
bilal.abdeen 02 Apr, 2016
Bob,

You are the best. It worked. Although one entry for the key and value worked before (see below.)
Car
Bus
...

It seems that the code, which displays the selected value needs the options to be properly formatted in pairs (see below.)
Car=Car
Bus=Bus
...

For anybody who is interested to take this further to display the values, which were read/retrieved from a database, you might need to write a code to convert back the array into a string before saving it back into the database. It seems that the (Handle Arrays) is NOT enough!, although still necessary. Moreover, if you are using the extension EasyProfile, you might need to add quotes (") around each array element and add brackets ([) & (]) around the whole string. You can use the following code.

// Add a quote (") around each array element
$i = 0;
foreach ($form->data[transport_meth] as $value)
{
$form->data[transport_meth][$i] = '"' . $value . '"' ;
$i++;
}

// Convert the array to a string
$form->data[transport_meth] = implode(",", $form->data[transport_meth]);

// Add ([) & (]) around the whole string
$form->data[transport_meth] = '[' . $form->data[transport_meth] . ']';

Good luck...
GreyHead 02 Apr, 2016
Hi bilal.abdeen,

You might try using json_encode() for the quoting and bracketing:
<?php
$form->data[transport_meth] = json_encode($form->data[transport_meth]);
?>
It looks as though that will have the same result as your code.
Bob
This topic is locked and no more replies can be posted.