HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Clarification needed for function CheckBox

That CheckBox function behaves exactly the other way around than I would expect it. Could anybody enlighten me and help me find the correct point of view in order to understand why it is working like that?

$Sender->Form->CheckBox(
    'TestFieldTrue',
    'Test Field True',
    array('value' => true) // Shows an unchecked checkbox
);

$Sender->Form->CheckBox(
    'TestFieldFalse',
    'Test Field False',
    array('value' => false) // Shows a checked checkbox
);

If I use array('value' => $Sender->Table->Column) I get unchecked CheckBoxes in my form for value 1 and checked CheckBoxes for value 0 and I don't know why it is done that way...

Comments

  • peregrineperegrine MVP
    edited January 2015

    what happens if you use - just wondering if this works or works as you expect.

    $Sender->Form->CheckBox(
    'TestFieldFalse',
    'Test Field False',
    array('checked' => "checked")
    

    as far as value - the way I see it working in other vanilla staff checkbox core stuff

    array('Value' => 1) - is unchecked see registration forms.

      $Value = ArrayValueI('value', $Attributes, true);
          $Attributes['value'] = $Value;
          if ($this->GetValue($FieldName) == $Value)
             $Attributes['checked'] = 'checked';
    
    
    
    
    
       function ArrayValueI($Needle, $Haystack, $Default = FALSE) {
          $Return = $Default;
          if (is_array($Haystack)) {
             foreach ($Haystack as $Key => $Value) {
                if (strtolower($Needle) == strtolower($Key)) {
                   $Return = $Value;
                   break;
                }
             }
          }
          return $Return;
       }
    }
    
    
        function GetValue($Key, &$Collection, $Default = FALSE, $Remove = FALSE) {
                $Result = $Default;
                if(is_array($Collection) && array_key_exists($Key, $Collection)) {
                    $Result = $Collection[$Key];
                 if($Remove)
                    unset($Collection[$Key]);
                } elseif(is_object($Collection) && property_exists($Collection, $Key)) {
                    $Result = $Collection->$Key;
                 if($Remove)
                    unset($Collection->$Key);
              }
    
              return $Result;
            }
        }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • The checked = checked array is what I'm using right now, but it feels like a workaround:

    if ($sender->table->column) {
      $attributes = array('checked' => 'checked');
    } else {
      $attributes = array();
    }
    
    $Sender->Form->CheckBox(
      'UnknonValueField',
      'Depends...',
      $attributes
    );
    
  • This is not a work-around, because that is what the spec asks for: http://www.w3.org/TR/html-markup/input.checkbox.html

    The value attribute is what is submitted to the server if the box is checked.

    I am interested in seeing the why the checkbox is being checked at all when you change the value though.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @hgtonight said:
    I am interested in seeing the why the checkbox is being checked at all when you change the value though.

    I guess it is that piece of code:

    if ($this->GetValue($FieldName) == $Value)
             $Attributes['checked'] = 'checked';
    

    GetValue returns "false" by default. So if my Checkbox has no values, GetValue returns "false". If I set the value to "false", the above equation is true and therefore the checkbox gets checked although it should not.

    Correct...? I'm still not sure I understand forms at all.

  • @R_J that makes sense. Any checkbox that has a value of false will be checked by default. The kicker is that this creates a seeming inversion of logic.

    Perhaps a patch that changes the line to if ($this->GetValue($FieldName, NULL) === $Value) should be tested. Would prevent the seeming inversion of logic.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Well, if I'm just doing it the wrong way, I'm simply doing it wrong. It might be nice not to confuse people who are already heading in the wrong direction, but it is not needed.
    Maybe some more comments in the @param array $Attributes line would be more helpful.

    Since a checkbox in most cases denotes true/false, I'd love to see that value = false renders an unchecked checkbox and value = true renders a checked one.
    But if I understand it right, I always have to give the value of 1 into the form control and if the CheckBox control is not in my form fields, I'll have to set its value to 0 so that it is saved correctly to the tinyint column that I have to use for boolean values.

  • peregrineperegrine MVP
    edited January 2015

    but "checked" is the correct usage.> @R_J said:

    glad checked worked for you. I'm not sure why you would have a value in related to a checkbox, but perhaps
    it was pre usage of the proper way to check boxes via "checked" in forms and to allow old plugins and code to work.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Checkboxes are very similar to radio inputs and share the same attributes.

    Even then, you may be constructing an attribute list with the checkboxes. Set the value to the attribute name and you will get exactly the data you want rather than creating a new parser for your specific items.

    Not exactly a great use case, but I am sure there are more.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

Sign In or Register to comment.