Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 Restarted my computer.... looks like it's working now... Could it be some kind of cache/php issue... totally lost Oké, I hope we all forget this.... 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 Big thanks for all your time, Soma, Adrian, Willy Kongondo you guys are awesome.... Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 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. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 To give an idea: 2 Link to comment Share on other sites More sharing options...
kongondo Posted March 30, 2014 Share Posted March 30, 2014 ...It only took 26 replies + 120 views + restarting a computer to resolve this..! Hehe... Glad u got it sorted... That looks pretty - well worth the effort 2 Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Maybe it will help somebody. Jaja. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 I love the autocheck, what car you have, can we check it out ? And indeed: public function __construct() Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 31, 2014 Author Share Posted March 31, 2014 The issue continues, on my work machine (same code as the fixed code from yesterday, there must be something with PHP ) pphff... I'll replace the checkboxes with radios.... just to be sure.. at home php 5.5, here php 5.4, PW: 2.4.1 Link to comment Share on other sites More sharing options...
Raymond Geerts Posted April 1, 2014 Share Posted April 1, 2014 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 More sharing options...
Raymond Geerts Posted April 1, 2014 Share Posted April 1, 2014 Figured it out $field->attr('value', is_array($data['subscriber_number']) ? $data['subscriber_number'] : array()); 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 (edited) 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 April 2, 2014 by Martijn Geerts 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 Pete, can you do something about the state "best answer", "solved" is better I guess if the press the button solved , Link to comment Share on other sites More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 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 More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 @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 More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 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 More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 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"); 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 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 More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 It's a feature not a bug. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 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 More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 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. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted April 2, 2014 Author Share Posted April 2, 2014 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 More sharing options...
Soma Posted April 2, 2014 Share Posted April 2, 2014 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. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now