如何解决Symfony:多种形式
| 我有以下(简化)表:Service:
product_name
number_of_clients
有人选择并保存带有3个客户端的服务A和带有2个客户端的服务B后,我必须显示一个页面,其中包含服务A的详细信息以及3个客户端的输入字段(名称和电子邮件),然后显示服务B的详细信息以及2个客户端的输入字段。在此页面上,有关客户机的信息将保存在数据库的另一个表中(强加的DB结构)。
如何创建随机数的表单以显示在每个服务下,之后如何正确访问提交表单中的数据?所有表单都可以同时编辑和保存,而不是单独编辑。
我查看了高级表格,但是并没有太大帮助。
解决方法
这是使用此简单架构的示例。您应该能够根据需要进行调整。
//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\">
而已。希望你可以把它们放在一起=)
,如果我对您的问题给出的答案太简单了,则表示歉意,但是您当然可以在相同的表单类的基础上以不同的方式命名/声明表单:
$form_1 = $form_2 = $form_3 = new someForm();
如果表单的数量未知,我将仅使用某种类型的动态参数或标识符来创建一组名称,以便在用户将其回发给您时使用。
然后,您可以分别访问每个提交的表单对象的数据(可能通过循环或类似的构造),就像处理单个表单一样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。