0

我有以下(简化)表:

Service:
  product_name
  number_of_clients

在有人选择并保存具有 3 个客户的服务 A 和具有 2 个客户的服务 B 后,我必须显示一个页面,其中包含服务 A 的详细信息和 3 个客户的输入字段(姓名和电子邮件),然后是服务 B 的详细信息和 2 个客户的输入字段。从这个页面,关于客户端的信息被保存在数据库的另一个表中(强加的 DB 结构)。

如何创建随机数量的表单以显示在每个服务下,以及之后如何正确访问提交表单中的数据?所有表格都是同时编辑和保存的,而不是单独保存。

我浏览了高级表格,但没有太大帮助。

4

2 回答 2

1

这是这个简单模式的示例。您应该能够根据自己的需要进行调整。

//schema.yml

Client:
  columns:
    name: string(255)
    service_id: integer
  relations:
    Service:
      local: service_id
      foreign: id
      foreignAlias: Clients

Service:
  columns:
    name: string(255)

//routing.yml

//i added this route to split 'new' action in 2 steps
service_choose:
  url: /service/choose
  param: {module: service, action: choose}

service:
  class: sfDoctrineRouteCollection
  options:
    model: Service
    module: service

//create a module based on service route collection (there's a task) and add choose action:
  public function executeChoose(sfWebRequest $request)
  {
    $form = new ServiceChooseForm();
    if (($method = $this->request->getMethod()) == 'POST')
    {
      $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

      if ($form->isValid())
      {
        $total_clients = $form->getValue('total_clients');
        $this->redirect('@service_new?total_clients='.$total_clients);
      }      
    }
    $this->form = $form;
    $this->setTemplate('choose');
  }

您还需要为选择操作创建一个自定义 sfForm 表单,您可以在其中设置多个客户端。

class ServiceChooseForm extends sfForm
{
  public function configure()
  {
    $this->widgetSchema->setNameFormat('service[%s]');

    $this->setWidget('total_clients', new sfWidgetFormInput());
    $this->setValidator('total_clients', new sfValidatorInteger());
  }
}
//ServiceForm embeds your new clients forms based on total_clients option.
class ServiceForm extends BaseServiceForm
{
  public function configure()
  {
    //clients sub form
    $clientsForm = new sfForm();
    for ($i=0; $i<$this->getOption('total_clients',2); $i++)
    {
      $client = new Client();
      $client->Service = $this->getObject();

      $clientForm = new ClientForm($client);
      $clientsForm->embedForm($i, $clientForm);      
    }
    $this->embedForm('newClients', $clientsForm);
  }
}

还有一些调整:

//chooseSuccess template, poinst a form to the same action:
<h1>New Service</h1>

<form action="<?php echo url_for('service_choose') ?>" method="post">
  <table>
    <tfoot>
      <tr>
        <td colspan="2">
          <?php echo $form->renderHiddenFields(false) ?>
          <input type="submit" value="Continue" />
        </td>
      </tr>
    </tfoot>
    <tbody>
      <?php echo $form ?>
    </tbody>
  </table>
</form>

//_form partial:

<form action="<?php echo url_for('@service_create?total_clients='. $form->getOption('total_clients')) ?>" method="post">

就是这样。希望你能把它们放在一起=)

于 2011-04-16T16:40:31.200 回答
0

抱歉,如果我对您的问题给出了过于简单的答案,但您当然可以基于相同的表单类以不同的方式命名/声明表单:

$form_1 = $form_2 = $form_3 = new someForm();

如果表单的数量未知,我只需使用某种类型的动态参数或标识符来创建一组名称,您可以在用户回发它们时使用它们。

然后,您可以单独访问每个提交的表单对象的数据(可能通过循环或类似的构造),就像您对单个表单所做的那样。

于 2011-04-15T15:57:53.677 回答