Martijn Geerts Posted March 30, 2014 Share Posted March 30, 2014 start edit: if someone else experience issues with checkboxes, here's the code that solves it for me end Am I doing something stupid, or is it broken? Because code below won't function. $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'fname'); $field->attr('value', 1); $field->attr('checked', ($this->fname === 1 ? 'checked' : '')); // $this->fname is an int I'm using this in ___getConfigInputfields. For other types of fields it works: $field = $this->modules->get('InputfieldRadios'); $field->attr('name', 'fname'); $field->addOption(1, __('Yep')); $field->addOption(0, __('Nope')); $field->attr('value', $this->fname); Link to comment Share on other sites More sharing options...
WillyC Posted March 30, 2014 Share Posted March 30, 2014 what u.get when u inspect markups ? mabe cannot.have chacked attr at all even blank one ? i alwayz only sets checked attr likes this if( $this->fname ) $field->attr( 'checked', 'chacked' ); Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 if( $this->fname ) $field->attr( 'checked', 'checked' ); Same outcome, doesn't work. :-( Link to comment Share on other sites More sharing options...
kongondo Posted March 30, 2014 Share Posted March 30, 2014 (edited) It works for me like this (just like yours) $checked = 1; $chx = $this->modules->get('InputfieldCheckbox'); $chx->attr('id+name', 'delete'); $chx->attr('checked', ($checked === 1 ? 'checked' : ''));//works also with simple == $chx->label = $this->_('Delete'); //more code then render... $m->attr('value', $chx->render());//$m = InputfieldMarkup //output <input type="checkbox" id="delete" name="delete" value="1" checked="checked"> If the checked attribute is blank (i.e. if it does not evaluate to 1) then checked attribute is not output in the resulting markup. Is that what is biting you? if your fname returning false? Edited:to add checking $checked variable;to show (newbies) the markup output Edited March 30, 2014 by kongondo Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 For what I remember from source (on mobile here) is that any value evaluate to true, is converted to 1 for the checkbox. So you code looks like that if it has markup, render the checkbox... Gonna check this out when I'm able to get back to the computer tonight. Link to comment Share on other sites More sharing options...
adrian Posted March 30, 2014 Share Posted March 30, 2014 Martijn, That does sound weird. The one thing in your code that looks off to me is this: $field->attr('value', 1); Won't that override the value every time the form is built? I have always done this: $field->attr('value', $this->fname ? $this->fname : 0 ); Link to comment Share on other sites More sharing options...
kongondo Posted March 30, 2014 Share Posted March 30, 2014 (edited) I'm not sure I get you Martijn...I've tried with both evaluate to true and to false (i.e. $checked=0 and $checked=1) and works as expected. Edit: FWIW, I have not tested this in the context of ___getConfigInputfields. Edited March 30, 2014 by kongondo Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Checkboxes value always is 1 whether checked or not .. Its the name that counts. For me Willie is working fine. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 Unchecked checkbox have no value I think. Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 It will not get sent if not checked .. naturally. Just value=1 in source I mean. Unless u set it to something else. Link to comment Share on other sites More sharing options...
kongondo Posted March 30, 2014 Share Posted March 30, 2014 Yes, for both 'checked' = 'checked' and where 'checked' has not been sent to the output markup, value = 1 [but you can set your own as Soma's says] Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Also === 1, won't work cause values in post are not integers but strings... small but big difference. So for checkboxes you check if is set in post and not for value usually. Link to comment Share on other sites More sharing options...
adrian Posted March 30, 2014 Share Posted March 30, 2014 Also === 1, won't work cause values in post are not integers but strings... small but big difference. So for checkboxes you check if is set in post and not for value usually. That was my initial thought, because for checkbox checking I have been using == "1", but I tested with === 1 and that works fine for me too. In this case $this->fname is not coming directly from the POST, so it shouldn't matter, no? Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 Exactly Adrian. If i var_dump it it tells me int. there people over here so, if they leave, will do more tests Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 value === 1 only works if the value is an 1 integer, which is not when submitted via a form post/get. value === "1" would. So what does $this->fname contain? and if that's 1 it does work. So does it work or not? I would make sure that $this->fname is really set at that time. So there's no issue cause it's working fine everywhere in core and my modules, so it's an issue elsewhere and between chair and computer. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 value === 1 only works if the value is an 1 integer, which is not when submitted via a form post/get. ( value coming from object, InputfieldJson ) So what does $this->fname contain? $this->message(gettype($this->field_numeric) . " - value: " . $this->field_numeric); // message: InputfieldJson: integer - value: 1 Somehow it doesn't work in the context of ___getConfigInputfields(). It does however work in the context of getModuleConfigInputfields I'll settle with the InputfieldRadios... still unsolved. ProcessWire 2.4.1 1 Link to comment Share on other sites More sharing options...
adrian Posted March 30, 2014 Share Posted March 30, 2014 Martijn, This is working for me: public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $f = $this->modules->get('InputfieldCheckbox'); $f->attr('name', 'input_country'); $f->label = 'Country Code'; $f->attr('value', $this->input_country ? $this->input_country : 0 ); $f->attr('checked', $this->input_country === 1 ? 'checked' : '' ); $f->description = 'Whether to ask for country code when entering phone numbers.'; $inputfields->append($f); Maybe you can see something in there that's different to what you are doing? Sorry, I can't think of anything else to try! 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 You're the best Adrian. Soma pointed to the real bug... now I have to find out where it goes wrong (your code works) It's all kinda weird, it not the first time I use Checkboxes... Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 In getModuleConfigInputfields – $this doesn't work cause it's static. Then you think the context is the problem? If $this->fname resolves to 1 then it does work in your code or all those modules that use this wouldn't work either: https://github.com/somatonic/ColorPicker/blob/master/FieldtypeColorPicker.module#L116 https://github.com/somatonic/AutoSave/blob/master/AutoSave.module#L95 Also looking at your code, I'm wondering why $this->fname? I don't think this works in there, unless you set the fname in the construct or init. $this->fieldname doesn't work for me anyway, so shouldn't it be $field->fname? But wondering if you can share the whole code to see not just the checkbox. Is it a fieldtype or what? Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Martijn, This is working for me: public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $f = $this->modules->get('InputfieldCheckbox'); $f->attr('name', 'input_country'); $f->label = 'Country Code'; $f->attr('value', $this->input_country ? $this->input_country : 0 ); $f->attr('checked', $this->input_country === 1 ? 'checked' : '' ); $f->description = 'Whether to ask for country code when entering phone numbers.'; $inputfields->append($f); Maybe you can see something in there that's different to what you are doing? Sorry, I can't think of anything else to try! Urhm? $f->attr('value', $this->input_country ? $this->input_country : 0 ); you don't ever need to set 1 or 0 on checkbox fields! It's the name that you check for not the value. So seems unessecary to me. $f->attr('checked', $this->input_country === 1 ? 'checked' : '' ); $this doesn't work in my book in ColorPicker for example, it's in the $field argument sent to getConfigInputfields($field) Ah, heh just seeing my ColorPicker also has set "value" on checkbox but as said it's not necessary, and that code is old meanwhile. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 You've the wrong settings in your mind Soma, it's not the settings from ConfigurableModules which are static, these ( Input field settings are not. ) 1 Link to comment Share on other sites More sharing options...
adrian Posted March 30, 2014 Share Posted March 30, 2014 I know I had some confusion with checkboxes in PW and I think I tried several things to get them working as expected. I think since then I have been blindly copying existing code. Point noted though and just to confirm. This actually works just fine $f = $this->modules->get('InputfieldCheckbox'); $f->attr('name', 'input_country'); $f->label = 'Country Code'; $f->attr('checked', $this->input_country ? 'checked' : '' ); $f->description = 'Whether to ask for country code when entering phone numbers.'; $inputfields->append($f); No need to set the value at all - just like you said 1 Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Anyway, this would be an form example which wouldn't be different from if in a module or not if($input->post->checkme){ $content .= "checked {$input->post->checkme}"; } $form = $modules->get("InputfieldForm"); $form->attr("method", "post"); $form->attr("action", "./"); $f = $modules->InputfieldCheckbox; $f->label = "checkme"; $f->attr("id+name","checkme"); $f->attr("checked", ($input->post->checkme ? "checked" : "")); $form->add($f); $f = $modules->InputfieldSubmit; $f->label = "send"; $form->add($f); $content .= $form->render(); You've the wrong settings in your mind Soma, it's not the settings from ConfigurableModules which are static, these ( Input field settings are not. ) So please don't hestitate to share your complete code, or we are screwed! And my example links are to examples of Fieldtypes/Inputfields ... Link to comment Share on other sites More sharing options...
Martijn Geerts Posted March 30, 2014 Author Share Posted March 30, 2014 Will do Soma, ( Would have posted this already if I don't have this stupid issue over here.... ) Adrian's example worked, until I changed the name again. makes me mad.... Link to comment Share on other sites More sharing options...
Soma Posted March 30, 2014 Share Posted March 30, 2014 Ah so it's an Inputfield heh... I see now. There's a lot of ways and traps for configuration with Fieldtypes/Inputfield. ... untill you changed the name? 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