0

我的插件元素中有一个表单,我想将复选框值插入名为 it_queries 和字段 status_type 的表中,它给了我一个错误 Undefined variable: variableValue [APP\Plugin\Feedback\View\Elements\comment_add.ctp ,第 37 行]。我已经像这样在我的控制器中声明了变量

$this->set('variableValueStatus', 'Pending');

这是第 37 行,这给了我错误

下面是控制器代码

    App::uses('FeedbackAppController', 'Feedback.Controller');

class CommentsController extends FeedbackAppController
{
public $components = array('Feedback.Comments');

public function add($foreign_model = null, $foreign_id = null)
{
if (empty($foreign_model) ||
empty($foreign_id) ||
!$this->request->is('post')
)
{
foreach ($_POST['likebutton'] as $pageId => $likeFlag) {
$dbFlag = $likeFlag ? 'Yes' : 'No';
}
return $this->redirect('/');
}

App::uses($foreign_model, 'Model');
$Model = ClassRegistry::init($foreign_model);

if (!($Model instanceof Model))
{
return $this->redirect('/');
}

if ($Model->hasAny(array($Model->primaryKey => $foreign_id)) == false)
{
return $this->redirect('/');
}

if (!isset($this->request->data['Comment']['foreign_model']) ||
!isset($this->request->data['Comment']['foreign_id']) ||
$this->request->data['Comment']['foreign_model'] != $foreign_model ||
$this->request->data['Comment']['foreign_id'] != $foreign_id)
{
return $this->redirect('/');
}

$user_id = null;

if (isset($this->Auth))
{
$user_id = $this->Auth->user('id');
}

$this->request->data['Comment']['foreign_model'] = $Model->name;
$this->request->data['Comment']['foreign_id'] = $foreign_id;
$this->request->data['Comment']['user_id'] = $user_id;


$this->Comment->create();

if (!$this->Comment->save($this->request->data))
{
$this->set('validation_errors', $this->Comment->validationErrors);
return;
}

$this->redirect($this->request->referer().'#comment-'.$this->Comment->id);
}
}

在我元素的添加视图中,这是我尝试访问变量值的方式

echo $this->Form->checkbox('ItQuery.status_type', array('type' => 'hidden', 'value'=>$variableValueStatus));

如果有人可以告诉我如何解决这个问题,那就太棒了

4

1 回答 1

0

您只在 !post 上传递变量,并且如果未设置外部模型和外部 id。在大多数情况下,这很可能不起作用。如果您在使用它之前没有在视图中检查它,那么您应该始终传递一个变量。

但无论如何,它仍然是错误的。如果您不希望表单回退到 POST 上的旧值(它不应该),则必须使用“默认”而不是“值”。

此外,最好使用 $this->request->data 来正确填写表单的默认值:

if (!$this->request->is('post')) {
    $this->request->data['ItQuery']['status_type'] = 'Pending';
}

查看使用表单

于 2013-03-06T07:52:39.450 回答