蛋糕.每页多个表单


CakePHP. Multiple Forms per page

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

附加信息(信息是我的主要新闻模式,它与Commodal一起加入)

索引.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(); ?>
控制器

通信控制器.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;
    }}

和模型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'
        )
    )
); */
}

我认为您无法在视图中访问$this->request->data(数据应使用表单输入,未提交)。您应该使用隐藏字段来传递诸如 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'].

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