PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 14, 2022

[FIXED] CakePHP3.3.15: FormHelper doesn't generate the right checkbox code

 February 14, 2022     cakephp-3.0     No comments   

Issue

I'm fighting with checkbox where CakePHP doesn't generate the right code and I can't understand why:

I initialize entities like that in my controller:

public $defaultPermissionFields = [
    /*
     * Module, item, item_visible, item_editable
     */
    ['item_visible' => 0],
    ['item_visible' => 1],
];

$permissions = $this->Permissions->newEntities($defaultPermissionFields);

So I have the following code in the ctp file:

<?= $this->Form->create($permissions, ['horizontal' => true])?>
    <fieldset>
        <div class="table-responsive well">
            <table class="table table-bordered table-striped">
                <?php foreach ($permissions as $key => $permission):?>
                    <tr>
                        <td class="text-center">
                            <?= $this->Form->checkbox('permissions.' . $key . '.item_visible', ['label' => false, 'value' => $permission->item_visible, 'required' => false]);?>
                        </td>
                    </tr>
                <?php endforeach;?>
            </table>
        </div>
    </fieldset>
    <?= $this->Form->button(__("Save"), ["class" => "btn btn-primary btn-block"]) ?>
<?= $this->Form->end(); ?>

And strangely, the following code is generated:

<table class="table table-bordered table-striped">
    <tr>
        <td class="text-center">
            <input type="hidden" name="permissions[0][item_visible]" value="0"/>   <-- WHY ???
            <input type="checkbox" name="permissions[0][item_visible]" value="0">
        </td>
    </tr>

    <tr>
        <td class="text-center">
            <input type="hidden" name="permissions[1][item_visible]" value="0"/>
            <input type="checkbox" name="permissions[1][item_visible]" value="1" checked="checked">
        </td>
    </tr>
</table>

As you can see, for the second row, the checkbox value is 1 and it's checked and it's hidden value is 0. That's the expected code. But For the first row, the checkbox value is 0 AND the hidden value is also 0 instead of 1.

Can you tell me why?

The second problem I have, but maybe caused by the first one is that I need to add required => false because whithout it, if I uncheck a checkbox, the browser tells me that I need to check the box !?!


Solution

You seem to be mixing up things. Why would you expect the hidden value to be anything other than 0? Your checkbox obviously represents a boolean value, so by default the fallback value should be 0, which represents the "not selected" state.

The actual problem is that the checkbox value should be 1 instead of 0. It is 0 because you're using the wrong option, value is not ment to be used to determine the checked state, but to actually set the value attribute value. What you want to use is either the checked, or the default option, if any at all, given that the form helper should be able to read the value from the entities on its own.

Quote from the docs:

  • checked - boolean indicate that this checkbox is checked.

[...]

  • default - Set the default value for the checkbox. This allows you to start checkboxes as checked, without having to check the POST data. A matching POST data value, will overwrite the default value.

Given that you probably want to keep user selections until the entity is actually being saved, you'd use the default option - if at all.

Also note that when passing the name of the base table to your inputs (that isn't actually required), make sure to use proper camel cased notation (ie. Permissions instead of permissions), as this does not represent an entity property!



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing