lundi 23 octobre 2017

Array to string conversion error in checkbox

Using CakePHP 3.5.3

Hi,

// WHAT I HAVE

I have a users index view which displays all the relevant users in the database. At the top of the page I have a select list which allows the user to select the deactivate option and on each users row I have a checkbox.

// WHAT I'M TRYING TO DO

The user can check the checkboxes they want then select the deactivate option from the select list and click submit. The submit sends the script to my actions method in my users controller. In the actions method I check to see if the deactivate option has been selected and if it hasn't I send a message back to the user to that effect. After this I check to see if a checkbox has been checked and if none have I send a message back to the user to that effect. If at least one checkbox has been checked and the deactivate option has been selected I then identify which checkboxes have been checked and update the database to deactivate for those choosen users.

// ON USERS INDEX VIEW I DECLARE MY CHECKBOX AS BELOW

<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>

// ACTIONS METHOD

public function actions()
{  
    if (empty($this->request->getData('action'))) {
        $this->Flash->sys_error('', [
            'key' => 'message',
            'params' => [
                'p1' => __('Please select an action!')
            ]
        ]);    

        // Send to index action for page load
        $this->setAction('index');
    }
    else {

        $checkboxArray = $this->request->getData('checkboxes');
        foreach($checkboxArray as $key => $val):

            echo 'key is ' . $key . "<br />";
            echo 'val is ' . $val . "<br />";

            if (empty($val)) {
               unset($checkboxArray[$key]);
            }
        endforeach;

        if (empty($checkboxArray)) {
            $this->Flash->sys_error('', [
                'key' => 'message',
                'params' => [
                    'p1' => __('Please select a user!')
                ]
            ]);

            // Send to index action for page load
            $this->setAction('index'); 
        }
        else {

            foreach($this->request->getData('checkboxes') as $userID):
                if ($userID != 0) {
                    // UPDATE THE DATABSE
                    echo 'in update the database ' . '<hr />';
                }
            endforeach;

            // Send to index action for page load
            $this->setAction('index');
        }
    }   
}

Even though I'm not 100% sure if this the best or correct way to do this it does work except for the following error which occurs on the checkbox in the users index view each time I invoke $this->setAction('index'); The stacktrace from the log is as follows:

Notice (8): Array to string conversion in [C:\xampp\htdocs\app\vendor \cakephp\cakephp\src\View\Widget\CheckboxWidget.php, line 81] Request URL: /users/actions Referer URL: https://localhost/app/users Trace: Cake\Error\BaseErrorHandler::handleError() - CORE\src\Error\BaseErrorHandler.php, line 153 Cake\View\Widget\CheckboxWidget::_isChecked() - CORE\src\View\Widget\CheckboxWidget.php, line 81 Cake\View\Widget\CheckboxWidget::render() - CORE\src\View\Widget\CheckboxWidget.php, line 51 Cake\View\Helper\FormHelper::widget() - CORE\src\View\Helper\FormHelper.php, line 2775 Cake\View\Helper\FormHelper::checkbox() - CORE\src\View\Helper\FormHelper.php, line 1570 include - APP/Template\Users\index.ctp, line 193 Cake\View\View::_evaluate() - CORE\src\View\View.php, line 1196 Cake\View\View::_render() - CORE\src\View\View.php, line 1157 Cake\View\View::render() - CORE\src\View\View.php, line 765 Cake\Controller\Controller::render() - CORE\src\Controller\Controller.php, line 623 Cake\Http\ActionDispatcher::_invoke() - CORE\src\Http\ActionDispatcher.php, line 125 Cake\Http\ActionDispatcher::dispatch() - CORE\src\Http\ActionDispatcher.php, line 93 Cake\Http\BaseApplication::__invoke() - CORE\src\Http\BaseApplication.php, line 103 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\Http\Middleware\CsrfProtectionMiddleware::__invoke() - CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 106 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\I18n\Middleware\LocaleSelectorMiddleware::__invoke() - CORE\src\I18n\Middleware\LocaleSelectorMiddleware.php, line 62 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\Routing\Middleware\RoutingMiddleware::__invoke() - CORE\src\Routing\Middleware\RoutingMiddleware.php, line 107 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\Routing\Middleware\AssetMiddleware::__invoke() - CORE\src\Routing\Middleware\AssetMiddleware.php, line 88 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\Error\Middleware\ErrorHandlerMiddleware::__invoke() - CORE\src\Error\Middleware\ErrorHandlerMiddleware.php, line 95 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 DebugKit\Middleware\DebugKitMiddleware::__invoke() - ROOT\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php, line 52 Cake\Http\Runner::__invoke() - CORE\src\Http\Runner.php, line 65 Cake\Http\Runner::run() - CORE\src\Http\Runner.php, line 51 Cake\Http\Server::run() - CORE\src\Http\Server.php, line 81 [main] - ROOT\webroot\index.php, line 40

// SOLUTION

I have managed to eliminate this error if I declare my checkbox as below:

// FROM
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>

// TO
<?= $this->Form->checkbox("checkboxes[$user->id]", ['value' => user->id]); ?>

// MY QUESTION

But this means I'm replacing the keys in the array with the user id and I don't know if this is considered as OK or if there's a better more professional way of doing it.

Any help would be greatly appreciated. Z.




Aucun commentaire:

Enregistrer un commentaire