复选框中的数组到字符串转换错误
Array to string conversion error in checkbox
使用 CakePHP 3.5.3
嗨,
// 我有什么
我有一个用户索引视图,它显示了数据库中的所有相关用户。在页面顶部,我有一个 select 列表,允许用户 select 停用选项,并且在每个用户行上我都有一个复选框。
// 我想做什么
用户可以选中他们想要的复选框,然后 select select 列表中的停用选项,然后单击提交。提交发送脚本
到我的用户控制器中的操作方法。在操作方法中,我检查停用选项是否已被 selected,如果没有,我会发送一个
将消息返回给用户。在此之后,我检查是否已选中一个复选框,如果 none 我向用户发送了一条消息来说明这一点。
如果至少选中了一个复选框并且停用选项已 selected 我然后确定哪些复选框已被选中并
更新数据库以停用那些选择的用户。
// 在用户索引视图中声明我的复选框如下
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// 动作方法
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');
}
}
}
尽管我不能 100% 确定这是否是最好或正确的方法,但它确实有效,除了每次调用时用户索引视图中的复选框出现以下错误 $this->setAction('index');
日志中的堆栈跟踪如下:
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
// 解决方案
如果我如下声明我的复选框,我已经设法消除了这个错误:
// FROM
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// TO
<?= $this->Form->checkbox("checkboxes[$user->id]", ['value' => user->id]); ?>
// 我的问题
但这意味着我要用用户 ID 替换数组中的键,我不知道这是否可行,或者是否有更好更专业的方法。
如有任何帮助,我们将不胜感激。 Z.
@SamHecquet - 感谢编辑,下次我会知道如何格式化堆栈跟踪。
@Derek - 按照要求,见下文 - 我不会包括整个页面,因为我认为没有必要,但如果你想要的比我 posted 多,请告诉我。
// Select 列表中的元素
<!-- Start form -->
<?php
// Declare individual page forms
if ($page === 'usersPage') {
// Users form
echo $this->Form->create(null, [
'url' => ['controller' => 'Users', 'action' => 'actions'],
'novalidate' => true
]);
}
<?php
// Declare individual page select lists
if ($page === 'usersPage') {
// Users form
$options = [
'' => '--Select Action--',
'a' => 'Deactivate User/s'
];
}
else{
header ("Location: ".$this->Url->build(["controller" => "Pages", "action" => "display", 'blank']));
exit(0);
}
$this->Form->label('action', '');
echo $this->Form->input('action', [
'type' => 'select',
'options' => $options,
'class' => 'ns-typ-2 ix-fa',
'label' => false
]);
?>
<?= $this->Form->submit(__('Go'), [
'class' => 'submit-ix-go'
]);
?>
// 索引用户
<!-- Headings -->
<div class="grid-ix-1 ix-r-top">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-h">
<div class="ix-h-cb">
<input type="checkbox" onClick="toggle(this)" >
</div>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-h">
<h2 class="ix-h2"><?= $this->Paginator->sort('Users.role', __('Role')) ?></h2>
</div>
</div>
</div>
<!-- Cells-->
foreach ($users as $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes[]', ['value' => $user->id]); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
<!-- Form ends -->
<?= $this->Form->end() ?>
@Derek - 请查看多复选框问题。
当我使用 Dave 的解决方案时,多个复选框 selection 在页面加载时起作用,但在我调用以下内容后不起作用:
// Send to index action for page load
$this->setAction('index');
我通过重定向而不是使用 setAction 解决了这个问题,如下所示:
// Redirect to index page
return $this->redirect(['action' => 'index']);
但是对于您的解决方案,多复选框 selection 功能在页面加载时或任何重定向后都不起作用。我使用以下代码来实现这一点。
// Javascript
// Check and uncheck all check boxes on index pages
function toggle(source) {
checkboxes = document.getElementsByName('checkboxes[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
// And then the heading checkbox is:
<input type="checkbox" onClick="toggle(this)" >
// And of course the cells checkbox
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id]); ?>
如果您对 javascript 有帮助,那很好,但如果没有,如果您建议可以找到解决方案,我很乐意 post 在 Whosebug javascript 部分寻求解决方案这意味着我可以使用您的解决方案而不编辑框架代码。
在此先感谢您的帮助或建议。 Z
它在错误的顶部提供了一个非常明确的答案:
Array to string conversion in [C:\xampp\htdocs\app\vendor
\cakephp\cakephp\src\View\Widget\CheckboxWidget.php, line 81
只需在您的 CheckboxWidget
中找到第 81 行,并修复指定的问题。
如果您不确定如何搜索 "php array to string conversion error",会提供许多结果,one of which 非常具体地描述了您的问题,涉及名为 checkboxes[]
.
的输入
尝试将 foreach 更改为:
<?php foreach ($users as $k => $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id, 'id' => 'checkbox' . $k, 'class' => 'userCheckbox']); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
Javascript 函数
function toggle(source) {
checkboxes = document.getElementsByClassName('userCheckbox');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
使用 CakePHP 3.5.3
嗨,
// 我有什么
我有一个用户索引视图,它显示了数据库中的所有相关用户。在页面顶部,我有一个 select 列表,允许用户 select 停用选项,并且在每个用户行上我都有一个复选框。
// 我想做什么
用户可以选中他们想要的复选框,然后 select select 列表中的停用选项,然后单击提交。提交发送脚本 到我的用户控制器中的操作方法。在操作方法中,我检查停用选项是否已被 selected,如果没有,我会发送一个 将消息返回给用户。在此之后,我检查是否已选中一个复选框,如果 none 我向用户发送了一条消息来说明这一点。 如果至少选中了一个复选框并且停用选项已 selected 我然后确定哪些复选框已被选中并 更新数据库以停用那些选择的用户。
// 在用户索引视图中声明我的复选框如下
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// 动作方法
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');
}
}
}
尽管我不能 100% 确定这是否是最好或正确的方法,但它确实有效,除了每次调用时用户索引视图中的复选框出现以下错误 $this->setAction('index');
日志中的堆栈跟踪如下:
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
// 解决方案
如果我如下声明我的复选框,我已经设法消除了这个错误:
// FROM
<?= $this->Form->checkbox("checkboxes[]", ['value' => $user->id]); ?>
// TO
<?= $this->Form->checkbox("checkboxes[$user->id]", ['value' => user->id]); ?>
// 我的问题
但这意味着我要用用户 ID 替换数组中的键,我不知道这是否可行,或者是否有更好更专业的方法。
如有任何帮助,我们将不胜感激。 Z.
@SamHecquet - 感谢编辑,下次我会知道如何格式化堆栈跟踪。
@Derek - 按照要求,见下文 - 我不会包括整个页面,因为我认为没有必要,但如果你想要的比我 posted 多,请告诉我。
// Select 列表中的元素
<!-- Start form -->
<?php
// Declare individual page forms
if ($page === 'usersPage') {
// Users form
echo $this->Form->create(null, [
'url' => ['controller' => 'Users', 'action' => 'actions'],
'novalidate' => true
]);
}
<?php
// Declare individual page select lists
if ($page === 'usersPage') {
// Users form
$options = [
'' => '--Select Action--',
'a' => 'Deactivate User/s'
];
}
else{
header ("Location: ".$this->Url->build(["controller" => "Pages", "action" => "display", 'blank']));
exit(0);
}
$this->Form->label('action', '');
echo $this->Form->input('action', [
'type' => 'select',
'options' => $options,
'class' => 'ns-typ-2 ix-fa',
'label' => false
]);
?>
<?= $this->Form->submit(__('Go'), [
'class' => 'submit-ix-go'
]);
?>
// 索引用户
<!-- Headings -->
<div class="grid-ix-1 ix-r-top">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-h">
<div class="ix-h-cb">
<input type="checkbox" onClick="toggle(this)" >
</div>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-h">
<h2 class="ix-h2"><?= $this->Paginator->sort('Users.role', __('Role')) ?></h2>
</div>
</div>
</div>
<!-- Cells-->
foreach ($users as $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes[]', ['value' => $user->id]); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
<!-- Form ends -->
<?= $this->Form->end() ?>
@Derek - 请查看多复选框问题。
当我使用 Dave 的解决方案时,多个复选框 selection 在页面加载时起作用,但在我调用以下内容后不起作用:
// Send to index action for page load
$this->setAction('index');
我通过重定向而不是使用 setAction 解决了这个问题,如下所示:
// Redirect to index page
return $this->redirect(['action' => 'index']);
但是对于您的解决方案,多复选框 selection 功能在页面加载时或任何重定向后都不起作用。我使用以下代码来实现这一点。
// Javascript
// Check and uncheck all check boxes on index pages
function toggle(source) {
checkboxes = document.getElementsByName('checkboxes[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
// And then the heading checkbox is:
<input type="checkbox" onClick="toggle(this)" >
// And of course the cells checkbox
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id]); ?>
如果您对 javascript 有帮助,那很好,但如果没有,如果您建议可以找到解决方案,我很乐意 post 在 Whosebug javascript 部分寻求解决方案这意味着我可以使用您的解决方案而不编辑框架代码。
在此先感谢您的帮助或建议。 Z
它在错误的顶部提供了一个非常明确的答案:
Array to string conversion in [C:\xampp\htdocs\app\vendor \cakephp\cakephp\src\View\Widget\CheckboxWidget.php, line 81
只需在您的 CheckboxWidget
中找到第 81 行,并修复指定的问题。
如果您不确定如何搜索 "php array to string conversion error",会提供许多结果,one of which 非常具体地描述了您的问题,涉及名为 checkboxes[]
.
尝试将 foreach 更改为:
<?php foreach ($users as $k => $user): ?>
<div class="grid-ix-1">
<!-- Checkbox -->
<div class="ix-usr-checkbox">
<div class="ix-c-cb">
<?= $this->Form->checkbox('checkboxes.' . $k, ['value' => $user->id, 'id' => 'checkbox' . $k, 'class' => 'userCheckbox']); ?>
</div>
</div>
<!-- Role -->
<div class="ix-usr-role">
<div class="ix-usr-role-text"><?= h($user->role) ?></div>
</div>
</div>
<?php endforeach; ?>
Javascript 函数
function toggle(source) {
checkboxes = document.getElementsByClassName('userCheckbox');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}