CakePHP的。每页多个表单

时间:2013-11-14 15:48:17

标签: php forms cakephp

我的网站上有很少的短消息,每个新闻我们都可以通过表格发表评论。而且我的问题出现了。 当我在一个表格中填写我的字段时,按下按钮后,所有表格都会重新加载而不保存,并且每个表格中的每个字段都必须填写,以便它们被视为一个部分如何避免它?

其他信息(信息是我主要的新闻模式,它与Com模式相结合)

index.ctp表格

<br><h5>Add comment:</h5><br>
                <?php echo $this->Form->create('Com'); ?>
                <?php echo $this->Form->input(__('mail',true),array('class'=>'form-control')); ?>
                <?php echo $this->Form->input(__('body',true),array('class'=>'form-control')); ?>
                <?php $this->request->data['ip'] = $this->request->clientIp(); ?>
                <?php $this->request->data['info_id'] = $info['Info']['id']; ?>
                <?php echo $this->Form->submit(__('Add comment',true),array('class'=>'btn btn-info')); ?>
                <?php $this->Form->end(); ?>

控制器ComsController.php

public function add()
{
    if($this->request->is('post'))
    {
        $this->Infos_com->create();
        $this->request->data['Infos_com']['ip'] = $this->request->clientIp();
        $this->request->data['Infos_com']['id_infos'] = $number;
        if($this->Infos_com->save($this->request->data))
        {
            $this->Session->setFlash(__('Comment is waiting for moderating',true),array('class'=>'alert alert-info'));
            return $this->redirect(array('controller'=>'Infos','action'=>'index'));
        }
        $this->Session->setFlash(__('Niepowodzenie dodania komentarza',true),array('class'=>'alert alert-info'));
        return TRUE;
    }}

和Model Com.php,我评论行以避免填写表格中的每个字段

class Com extends AppModel
{
public $belongsTo = array('Info');
/*public $validate = array(
    'mail'=>array(
        'requierd'=>array(
            'rule'=>array('notEmpty'),
            'message'=>'Write your email'
        )
    ),
    'body'=>array(
        'required'=>array(
            'rule'=>array('notEmpty'),
            'messages'=>'Write smth'
        )
    )
); */
}

1 个答案:

答案 0 :(得分:0)

我认为您无法在视图中访问$ this-&gt; request-&gt;数据(数据应使用表单输入,但未提交)。您应该使用隐藏字段来传递IP od id等参数...示例:

echo $this->Form->input('Infos_com.client_id', array( 'type' => 'hidden', 'value' => $value ));

如果您有多个表单,则分隔其字段会很有用。例如:

echo $this->Form->input('Infos_com.' . $news_id . '.body', array('label' => __('body')));

这样你就会得到一个像:

这样的数组

$this->request->data['Infos_com'][$news_id]['body'].

然后你可以在模型中制作你的逻辑。