nblgeoweb VIP
Total posts: 178
11 Янв 2017 16:43

There is currently a way to specify a default for a checkbox field but a checkbox field can have several values. Is it possible to have more than one default specified for a checkbox field?

Последние изменения: 17 Янв 2017


pepperstreet VIP
Total posts: 3,837
11 Янв 2017 19:08

Hello nblgeoweb, good point and feature request. As far as I can tell, you can enter a single number only. Multiple comma seperated list would make sense...

BTW, did you test a single default? I could not get any default setting on submission form. A bug?


nblgeoweb VIP
Total posts: 178
11 Янв 2017 21:09

A single default worked fine. My checkboxes each have text string values. I had to specify one of those text string values in the default parameter. I tried delimiting multiple text strings via commas, semi-colons, etc but no luck. It's all just interpreted as a single value. I haven't dug in the code yet to see if there is a way that I haven't thought of yet.


pepperstreet VIP
Total posts: 3,837
12 Янв 2017 06:14

nblgeoweb I had to specify one of those text string values in the default parameter.

Oh yeah, you are right. I confused it with another form component. Sorry. Forget about it.


I tested again with the exact "string value" as default. It works!
But only with a single value. Not possible to seperate with commas.

Not sure where this magic happens... if it is a form input template, then it might be possible to get a multi-value solution.


nblgeoweb VIP
Total posts: 178
12 Янв 2017 21:26

It seems the field is built in fields/checkbox/tmpl/input/default.php but $this->values is from fields/checkbox/checkbox.php with defaults being checked around line 47 with this code:

if($this->isnew && $this->params->get('params.default_val'))
{
         $this->value[] = $this->params->get('params.default_val');
}

Probably just need some code to parse $this->params->get('params.default_val')


nblgeoweb VIP
Total posts: 178
12 Янв 2017 21:52

How about something like this:

if($this->isnew && $this->params->get('params.default_val'))
{
        $this->value = array_merge($this->value,explode(",",$this->params->get('params.default_val')));
}

Seems to work. That assumes using a comma as a delimiter. On the negative side this would break for anyone that currently has put a comma in their specified default - not likely but possible.


nblgeoweb VIP
Total posts: 178
12 Янв 2017 22:00

I just noticed something... Just above this block $values get set to $this->values. Should that be $this->value (no s)? It looks like a typo if the getting values from sql should be working.


Sergey
Total posts: 13,748
16 Янв 2017 05:02

I cannot change it already That would breack backward compatibility. But you can add this

if($this->isnew && $this->params->get('params.default_val'))
{
        $this->value = array_merge($this->value,explode(",",$this->params->get('params.default_val')));
}

In to your custom input template.


nblgeoweb VIP
Total posts: 178
17 Янв 2017 12:44

Nice solution. Thanks Sergey.

Работает на Cobalt