<p>A checkbox field is just a checkbox, usually checkboxes don't have several values beside true/false or checked/unchecked, Are you sure you don't want to use a <code>select</code> or <code>multiselect</code></p>

<pre><code>{{ forms.selectField({
    label: "Some Label"|t,
    id: id ~ 'someField',
    name: name ~ '[someField]',
    options: options, //&lt;-- array [label =&gt; X, value =&gt; y]
    value: values.someField // &lt;-- not sure what this should be.. usually you want to pass an id
}) }}
</code></pre>

<p>However - your PHP actually doesn't make much sense. Not sure what your goal is with that code</p>

CC BY-SA 4.0
2018-08-02T08:19:42.693Z
Thank you very much for your effort. I'm having problem with display strings in the template(select or checkbox). I updated the code $add=array();
            foreach ($json as $v){
                $add[]= array(
                    'someField'     => array(AttributeType::String, 'default' => $v['name']),
                );
            }

            echo '<pre>'.print_r($add,1).'</pre>';
            return array_merge(parent::defineAttributes(), $add);



        }
S. BTM
CC BY-SA 4.0
2018-08-02T08:50:17.563Z
As I said, that makes no sense at all. Your options must be an array with a format like `[ [label => options 1, value = 1], [label => option 2, value 2]]`. Merging arrays in loops is a really bad idea to begin with
Robin Schambach
CC BY-SA 4.0
2018-08-01T14:41:20.593Z
2018-08-01T14:41:20.593Z
Robin Schambach
<p><strong>Solution:</strong>
After did some research in plugin development it worked for me, twig doesn't seem to accept php array so I pass string first and than explode the string with the returned value. Then in template, for checkbox(need to make different checkbox name to make boxes clickable),</p>

<pre><code>{% for check in options %}
{{ forms.checkboxField({
    id: id ~ 'someField',
    class: 'nicetext',
    name: name ~ '[someField]',
    value: check,
    errors: values.getErrors('someField'),
    required: false,
    label: check
}) }}{% endfor %}
</code></pre>

<p>and in model,</p>

<pre><code>$result = array();
foreach ($json as $key =&gt; $value){
    $result[] = $value['name'];
}
$projects = implode(",",$result);

return  array('someField' =&gt; array(AttributeType::Enum, 'default' =&gt; $projects),
);
</code></pre>

CC BY-SA 4.0
2018-08-02T14:53:21.703Z
**twig doesn't allow php array** -> of course it does
Robin Schambach
CC BY-SA 4.0
2018-08-02T14:54:23.173Z
Is it? directly from model?
S. BTM
CC BY-SA 4.0
2018-08-02T14:55:43.420Z
It doesn't matter where it comes from, you can proceed every array in Twig and can create new arrays as well. I guess you just use it wrong. Since your `someField` attribute especially the `default` doesn't seem to be used correctly
Robin Schambach
CC BY-SA 4.0
2018-08-02T14:59:12.920Z
I'm in learning phase, you may be right ,  however, for me  Twig does not seem to accept it if I return a PHP array. so I use implode explode to make things. Thank you very much for your guidance, anyway. :)
S. BTM
CC BY-SA 4.0
2018-08-02T14:49:47.323Z
2018-08-02T15:15:54.297Z
2018-08-02T15:15:54.297Z
S. BTM
<p>I'm able to get data using API but when I use checkboxField then it only display the blank checkbox without value. In Model,</p>

<pre><code>    if($result){
        if (isset($result-&gt;error)) die($result-&gt;error_message);
        //convert json data to array
        $arr = json_decode($result, true);

        //Get project array from API
        $json = $arr['projects'];

        foreach ($json as $i =&gt; $v){

            return array_merge(parent::defineAttributes(), array(
                'someField'     =&gt; array(AttributeType::String, 'default' =&gt; $v['name']),
            ));
        }
</code></pre>

<p>And my template, </p>

<pre><code>{% import "_includes/forms" as forms %}

{{ forms.checkboxField({
id: id ~ 'someField',
class:'nicetext',
name: name ~ '[someField]',
value: values.someField,
checked: value,
errors: values.getErrors('someField'),
required: false,}) }}
</code></pre>

<p>Need some help. TIA!</p>

<plugin-development><templating>
How to display data in field type with dropdown multi select checkbox?
# How to display data in field type with dropdown multi select checkbox?

(Asked by: username_0 on 2018-08-01)

I'm able to get data using API but when I use checkboxField then it only display the blank checkbox without value. In Model,
    if($result){
        if (isset($result->error)) die($result->error_message);
        //convert json data to array
        $arr = json_decode($result, true);

        //Get project array from API
        $json = $arr['projects'];

        foreach ($json as $i => $v){

            return array_merge(parent::defineAttributes(), array(
                'someField'     => array(AttributeType::String, 'default' => $v['name']),
            ));
        }

And my template, 
{% import "_includes/forms" as forms %}

{{ forms.checkboxField({
id: id ~ 'someField',
class:'nicetext',
name: name ~ '[someField]',
value: values.someField,
checked: value,
errors: values.getErrors('someField'),
required: false,}) }}

Need some help. TIA!

**Tags:** plugin-development, templating

---

## 2 answers

### Answer by username_1 on 2018-08-01. Score: 1

A checkbox field is just a checkbox, usually checkboxes don't have several values beside true/false or checked/unchecked, Are you sure you don't want to use a select or multiselect
{{ forms.selectField({
    label: "Some Label"|t,
    id: id ~ 'someField',
    name: name ~ '[someField]',
    options: options, //<-- array [label => X, value => y]
    value: values.someField // <-- not sure what this should be.. usually you want to pass an id
}) }}

However - your PHP actually doesn't make much sense. Not sure what your goal is with that code

> **Comments:**
> **username_0**: Thank you very much for your effort. I'm having problem with display strings in the template(select or checkbox). I updated the code $add=array();             foreach ($json as $v){                 $add[]= array(                     'someField'     => array(AttributeType::String, 'default' => $v['name']),                 );             }              echo '<pre>'.print_r($add,1).'</pre>';             return array_merge(parent::defineAttributes(), $add);            }  
> **username_1**: As I said, that makes no sense at all. Your options must be an array with a format like `[ [label => options 1, value = 1], [label => option 2, value 2]]`. Merging arrays in loops is a really bad idea to begin with  

---

### Answer by username_0 on 2018-08-02. Score: -1

Solution:
After did some research in plugin development it worked for me, twig doesn't seem to accept php array so I pass string first and than explode the string with the returned value. Then in template, for checkbox(need to make different checkbox name to make boxes clickable),
{% for check in options %}
{{ forms.checkboxField({
    id: id ~ 'someField',
    class: 'nicetext',
    name: name ~ '[someField]',
    value: check,
    errors: values.getErrors('someField'),
    required: false,
    label: check
}) }}{% endfor %}

and in model,
$result = array();
foreach ($json as $key => $value){
    $result[] = $value['name'];
}
$projects = implode(",",$result);

return  array('someField' => array(AttributeType::Enum, 'default' => $projects),
);

> **Comments:**
> **username_1**: **twig doesn't allow php array** -> of course it does  
> **username_0**: Is it? directly from model?  
> **username_1**: It doesn't matter where it comes from, you can proceed every array in Twig and can create new arrays as well. I guess you just use it wrong. Since your `someField` attribute especially the `default` doesn't seem to be used correctly  
> **username_0**: I'm in learning phase, you may be right ,  however, for me  Twig does not seem to accept it if I return a PHP array. so I use implode explode to make things. Thank you very much for your guidance, anyway. :)  

---

