1

我提出的以下(简单)问题:

  $user = new User();
  $user->setEventid(2);
  $user->setT(new \DateTime('now'));

  $em = $this->getDoctrine()->getManager();
  $em->persist($user);
  $em->flush();

  $form = $this->createFormBuilder($user)
    ->add('email', 'email')
    ->add('terms', 'checkbox', array(
          'label'     => 'Read terms?',
          'required'  => true))
    ->getForm();

这是我的代码。电子邮件是用户的财产,条款不是。我希望他们的用户保存他们的电子邮件并检查条款复选框,但我只需要保存电子邮件,而不是如果他们选中该框 - 如果他们不这样做,他们就无法提交表格。

你知道我的意思?我确信实现这一点非常简单;)

第二个问题:如何给渲染的 from-tag 一个 id-attribute 以便在 jquery 中进一步处理?

4

2 回答 2

2

要将实体中不存在的字段添加到表单中,请使用以下命令:

->add('terms', 'checkbox', array(
    'label'         => 'Read terms?',
    'required'      => true,
    'mapped'        => false, // this works since Symfony 2.1
    'property_path' => false, // this works since Symfony 2.0
))

Symfony 为每个表单元素生成一个 ID;只需打开呈现的页面源并查看所需元素的 ID。

于 2012-11-01T06:34:45.253 回答
0

如果您的实体中没有“条款”字段,则不会保存。对于您的第二个问题,将 id 设置为 twig 中的表单非常简单,只需:

{{ form_label(form.name, 'Your Name', { 'attr': {'id': 'foo'} }) }}
于 2012-10-31T22:43:46.217 回答