Jump to content

InputfieldCheckbox issue


Martijn Geerts
 Share

Recommended Posts

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

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 by kongondo
Link to comment
Share on other sites

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

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

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 by kongondo
Link to comment
Share on other sites

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

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

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

  • Like 1
Link to comment
Share on other sites

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!

  • Like 2
Link to comment
Share on other sites

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

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

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 :)

  • Like 1
Link to comment
Share on other sites

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

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...