Symfony:多种形式

发布于 2024-11-01 13:38:05 字数 458 浏览 4 评论 0原文

我有以下(简化)表:

Service:
  product_name
  number_of_clients

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

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

我浏览了高级表单,但它没有多大帮助。

I have the following (simplified) table:

Service:
  product_name
  number_of_clients

After someone selects and saves Service A with 3 clients and Service B with 2 clients I must display a page with details for Service A and input fields (name & email) for the 3 clients then details for Service B and input fields for 2 clients. From this page the informations about the clients are saved in another table in the database (imposed DB structure).

How can I create the random number of forms to display under each service and how can I properly access the data in the submitted form afterwards? All the forms are edited and saved at the same time, not individually.

I went over Advanced Forms but it didn't help much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-11-08 13:38:05

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

//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">

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

Here's an example with this simple schema. You should be able to adjust it to your needs.

//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');
  }

You also need to create a custom sfForm form for choose action, where you set a number of clients.

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);
  }
}

Some more tweaks:

//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">

That's it. Hope you can put it all together =)

归途 2024-11-08 13:38:05

如果我对你的问题给出一个过于简单的答案,我很抱歉,但是你当然可以根据相同的表单类以不同的方式命名/声明表单:

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

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

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

Apologies if I'm giving an over-simple answer to your question, but you can of course name/declare the forms differently on the basis of the same form class:

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

If the number of forms is unknown, I'd just use some type of dynamic parameter or identifier to create a set of names you can work with when they're posted back by the user.

You can then access the data for each submitted form object separately (maybe through a loop or similar construct), just as you would do with a single form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文