【问题标题】:How to save multiple forms on one view如何在一个视图中保存多个表单
【发布时间】:2013-09-23 05:14:43
【问题描述】:

情况是这样的。我们需要能够创建报价。报价包含组件:研究、Pesel 和预约、抄送和注册和设置费用。一个报价可能有也可能没有,但最多只能有 1 个。

我们在一个视图上有一系列表单,我们将这些表单划分为选项卡,以便一次只能查看一个。选项卡和第一个表单的代码在这里:

<script>
    $(function() 
    {`
         $( "#tabs" ).tabs();
    });
    $(function() {
    $( "#accordion" ).accordion({
        active: false,
        collapsible: true,
        heightStyle: "content"

    });
});

</script>

    <div id="tabs">
    <ul>
        <li><a href="#tabs-1">Quote Info</a></li>
        <li><a href="#tabs-2">Research</a></li>
        <li><a href="#tabs-3">CC and Registration</a></li>
        <li><a href="#tabs-4">PESEL and appointment</a></li>
        <li><a href="#tabs-5">Other Fees</a></li>
    </ul>

<div id="tabs-1">
<?php echo $this->Form->create('Quote'); ?>
        <p>
        <h3>Quote Information</h3>
        <p>
<fieldset>
    <div id="hide"><?php echo $this->Form->input('date', array('dateFormat' => 'DMY'));?></div>
    <?php
    echo $this->Form->input('description');
            echo $this->Form->input('quote_accepted');
    echo $this->Form->input('research_accepted');
    echo $this->Form->input('cc_accepted');
    echo $this->Form->input('pesel_accepted');
    echo $this->Form->input('setfees_accepted');
    echo $this->Form->input('total', array('value' => '0'));
?>
</fieldset>

<?php echo $this->Form->end(__('Submit')); ?>

第一种形式可以正常工作。一个不起作用的是这里:

<div id="tabs-5">

        <?php echo $this->Form->create('Quote'); ?>
        <h3><?php echo __('Other Fees'); ?></h3>
    <table>
        <tbody>
            <tr>
                <th>Item</th>
                <th>Quantity</th>
                <th>Initial Price</th>
                <th>Client Price</th>
                <th>Total</th>
            </tr>  
            <tr>
                <td><h5>Children under 18</h5></td>
                <td> <?php echo $this->Form->input('Setfee.children_quantity', array('label' => ''));?> </td>
                <td>$900</td>
                <td><input type="text"> </td>
                <td> <?php echo $this->Form->input('Setfee.children_total', array('label' => ''));?> </td>
            </tr>
                <tr>
                <td><h5>Relatives of Confirmed Citizens</h5></td>
                <td> <?php echo $this->Form->input('Setfee.relatives_quantity', array('label' => ''));?> </td>
                <td>$900</td>
                <td><input type="text"> </td>
                <td> <?php echo $this->Form->input('Setfee.relatives_total', array('label' => ''));?> </td>
            </tr> 
            <tr>
                <td><h5>Registration of One Birth or Marriage Certificate</h5></td>
                <td> <?php echo $this->Form->input('Setfee.reg_birth_marriage_quantity', array('label' => ''));?> </td>
                <td>$50</td>
                <td><input type="text"> </td>
                <td> <?php echo $this->Form->input('Setfee.reg_birth_marriage_total', array('label' => ''));?> </td>
            </tr> 
            <tr>
                <td><h5>Registration of Birth and Marriage Certificate Altogether</h5></td>
                <td> <?php echo $this->Form->input('Setfee.reg_birth_marriage_together_quantity', array('label' => ''));?> </td>
                <td>$50</td>
                <td><input type="text"> </td>
                <td> <?php echo $this->Form->input('Setfee.reg_birth_marriage_together_total', array('label' => ''));?> </td>
            </tr> 
             <tr>
                <td><h5>Standard Fees</h5></td>
                <td></td>
                <td></td>
                <td></td>
                <td> <?php echo $this->Form->input('Setfee.standard_fee_total', array('label' => ''));?> </td>
            </tr> 
        <tr>
        <td> <h5><?php echo $this->Form->input('Setfee.set_fees_total');?></h5></td>
        <td></td>
        <td></td>
        <td></td>
        <div id='hide' ><?php echo $this->Form->input('Setfee.quote_id');?></div>
        <td><?php echo $this->Form->end(__('Submit')); ?></td>
        </tr>     
        </tbody>
    </table>
        </div>

保存的控制器在这里:

public function makeQuote()
    {
        $this->loadModel('Applicant');
        $this->LoadModel('ApplicantQuote');
        $this->LoadModel('Setfee');

        //retrieves the applicants chosen from the view_quotes page
        $args = $this->params['url'];
        $applicants = $this->Applicant->find('all', array('conditions' => array('id' => $args), 
            'order' => 'first_name ASC', 'recursive' => -1));


        $this->set(compact('applicants'));

        if ($this->request->is('post')) 
            {
            $this->Quote->create();

            if($this->Quote->saveall($this->request->data))
            {
                $this->Session->setflash(__('quote saved'));
    }
    }

基本上,我们需要能够彼此独立地保存表单。至于型号。 Quotes 有一个固定的费用,抄送和注册,pesel 和研究,每一个都属于报价。

我们是 cakephp 的新手,我们已经走到了尽头。请帮忙。

【问题讨论】:

  • 什么不起作用?你遇到了什么错误?您确实意识到,即使您在一个页面上显示了多个表单,您也只能在给定的请求中提交一个?
  • 如果我们尝试提交除第一个以外的任何表单,则无法保存。它不显示错误,页面刷新并且没有任何反应。

标签: cakephp multiple-forms


【解决方案1】:

您的所有 5 个表单都在同一个页面上,具有相同的 ID,并提交给相同的控制器操作。因此,第一个会起作用,而在所有其他表单上单击提交只会提交第一个表单(即,其他表单不起作用)。

首先,我将它作为一个大而长的表格工作,没有标签 - 只是这样你一次处理一个问题。一旦这样做了,您就有几个选项可以将表单拆分为选项卡。最直接的是:

在选项卡开始前打开表单,在选项卡结束后关闭表单。提交按钮将位于选项卡之外,如下所示。但是字段本身可以根据您的需要分布在选项卡中。

用户将能够从一个选项卡转到另一个选项卡,编辑字段,然后通过单击选项卡外部的提交按钮保存所有字段,如下所示。

所以看起来像这样:

<?php echo $this->Form->create('Quote'); ?>

<div id="tabs-1">
    // A bunch of form fields go here
</div>
<div id="tabs-2">
    // A bunch of other form fields go here, etc
</div>

<?php echo $this->Form->end(__('Submit')); ?>

您的其他选择是让每个选项卡成为一个单独的表单,提交给单独的控制器操作,和/或使用 ajax。但是我之前描述的方法应该是最简单的。

【讨论】:

  • 原来问题是输入名称和模型名称之间的差异。感谢您的建议。
猜你喜欢
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2018-01-25
  • 2019-08-03
  • 1970-01-01
相关资源
最近更新 更多