Jump to content

InputfieldCheckbox issue


Martijn Geerts
 Share

Recommended Posts

So if it's an Inputfield config... you have a checkbox "fname" ?

Then you also have something like

$this->set('fname', '');

in the _construct of your inputfield? Or it won't work as $this->fname would be empty always.

I have same code as you and as soon as I remove, $this->set('fname', ''); from the construct, the checkbox won't get checked.

(This could also be set in the Fieldtype, getInputfield() where the Inputfield is loaded.) So couple ways to do configs.

Also I've seen in Checkbox module, you could set autocheck:

$field->attr('autocheck', 1);
$field->attr('value', $this->fname);
and it will get checked if value is not 0.
  • Like 1
Link to comment
Share on other sites

Any example on how this work with a ConfigurableModule when using InputfieldCheckboxes (the multiple checkboxes field)

I have this code, but got no idea how to set the values checked when they get saved, also couldnt find any examples on the forum so far.

$field = wire('modules')->get('InputfieldCheckboxes');
$field->name = 'subscriber_number';
$field->label = 'Subscriber number';
$field->addOption('visable', 'Visable');
$field->addOption('required', 'Required');
$field->addOption('editable', 'Editable');
$field->columnWidth = 50;
$fieldset->append($field);

In the database it saves the following json string when all values are checked when saving the module:

{"subscriber_number":["visable","required","editable"]}
Link to comment
Share on other sites

I've found the issue :)

If you predefine a default value of (int) 1 the value is not saved to the database. ( fields.data  )

If you predefine a value of (int) 0, it all works as expected.

Bug is reported to github.

Edited by Martijn Geerts
  • Like 1
Link to comment
Share on other sites

That means this doesn't work for you?

$field->attr('autocheck', 1);
$field->attr('value', $this->fname);

I can't reproduce this. Works fine.


There's no issue, as you don't specify value = 1 for checkbox. So don't do this:

$field->attr('value', 1);

Link to comment
Share on other sites

@Soma, same issue with your code:

// Pre-populate the data

// this doesn't work, NOT saved to fields.data (db)
public function __construct() {
	$this->set('fname', 1);
}

// this works, nicely saved to fields.data (db)
public function __construct() {
	$this->set('fname', 0);
}
Link to comment
Share on other sites

But that anyway a little special with checkboxes, you usually don't have it checked by default. 

Maybe I'm completely off but it's all a little tricky with checkboxes:

Since when a checkbox is not checked there will be no setting saved, it's "empty", not sent etc. So only when checked  it will have an value of 1 saved to db. Defining a default value of 1 in construct will not save it to db, only if you save the first time, but then it will always be 1 cause you overwrite it when it's loading.

So what you trying to do is: if checkbox is empty/not set make it checked? How is this gonna work anyway when there's no value saved when unchecked? So making a default of checkbox to 1 will give hard time as when it's not checked. Not going to work.

Apart from that, contrary to what you say, for me the value, when checked and saved, is save in db no matter what the default is.

Instead make it inverse, by default it's on and disable it when checked.

Link to comment
Share on other sites

Ok, looked at this situation as I wondered and had similar issues in the past.

If you really want to have a checkbox inputfield checked by default in this context you have to use the uncheckedValue, checkedValue property instead to make it work.

Following example works fine:

$f->attr('name', 'formatting');
$f->attr('uncheckedValue', "no");
$f->attr('checkedValue', "yes");
$f->attr('checked', $this->formatting == "yes" ? 'checked' : '');

This makes sure a unchecked value is saved, instead of nothing.

And default in construct then can be 

$this->set('formatting', "yes");
 
  • Like 2
Link to comment
Share on other sites

I confirm that your code works, never the less, I consider the issue as a bug.

// this works
$field = $this->modules->get('InputfieldCheckbox');
$field->attr('name', 'fname');
$field->attr('autocheck', 1);
$field->attr('uncheckedValue', 0);
$field->attr('checkedValue', 1);
$field->attr('value', $this->fname);
Link to comment
Share on other sites

If you start with the value of 1,  you unchecked it and saved, the value should be 0, and not unchanged

( If you start with the default of 0, it all works )

Never the less, after loads of post, a bug report, restarting computers, trying on different installs etc etc, I wil never forget, I will do checkboxes the way in the post above.

Thanks all...

Link to comment
Share on other sites

In case of checkbox, by default behaviour unchecked is NOT saved in DB, that's not a bug but a feature. No need to store a value, this is to either keep the logic of checkboxes and not waste space in DB.

This only is an issue if you want to make the checkbox checked by default, even if you won't find any checked checkboxse by default in PW.  Because it a better pattern to most of the time to inverse the logik and check a box to disable something, not the other way around.

But for this case of still wanting to do the default 1, you can set the checked and unchecked value to make sure unchecked is a value that is saved. 

  • Like 1
Link to comment
Share on other sites

That design does make sense. Every PW developer should know that, it's mentioned by you ryan and a lot of developers around the forum here.

But it doesn't pickup changes, if started with a value...  ( still weird )

(but we have a work around here) 

Link to comment
Share on other sites

But it doesn't pickup changes, if started with a value...  ( still weird )

Um no, It does save as it should for me. No matter If I specify 1 or 0 as the default in construct, if checked and saved, fname: 1 is saved to db (without the "workaround”) and when I uncheck and save the fname: 1 is cleared and no value in db, as it should. 

As said, in you don't specify a uncheckedValue it will always be checked, cause there's no value saved for unchecked and on load it will set 1, and not overwrite it as there's no value coming from db.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...