Symfony 如何更改choiceType输入字段的标签?
我正在处理symfony2.8,并试图根据需要显示choiceType(radiobutton)的标签(而不是doctrine根据用于存储输入数据的变量(如eT1或eT2)猜测的标签)。是否有相应的语法,或者我是否需要将变量名更改为要显示的名称,例如event1或event2。此外,如果我想显示“请为时段1选择事件”,而不是仅显示事件1,该怎么办Symfony 如何更改choiceType输入字段的标签?,symfony,doctrine-orm,Symfony,Doctrine Orm,我正在处理symfony2.8,并试图根据需要显示choiceType(radiobutton)的标签(而不是doctrine根据用于存储输入数据的变量(如eT1或eT2)猜测的标签)。是否有相应的语法,或者我是否需要将变量名更改为要显示的名称,例如event1或event2。此外,如果我想显示“请为时段1选择事件”,而不是仅显示事件1,该怎么办 <?php namespace AppBundle\Controller; use AppBundle\Entity\eve
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\events;
//use AppBundle\Entity\eventtype;
use AppBundle\Entity\users;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class DefaultController extends Controller {
/**
* @Route("/home", name="homepage")
*/
public function indexAction(Request $request) {
$events = new events();
$form = $this->createFormBuilder($events)
->add('eT1', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
))
->add('eT2', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
))
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->isValid()) {
// perform some action, eg. persisting the data to database...
$user = $this->container->get('security.context')->getToken()->getUser();
$events->setuser($user);
// var_dump($id);
// exit;
//$events->setuserID($id);
$em = $this->getDoctrine()->getManager();
// tells Doctrine you want to (eventually) save the Product (no queries yet)
$em->persist($events);
// actually executes the queries (i.e. the INSERT query)
$em->flush();
return $this->redirectToRoute('homepage');
}
}
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}
}
以下symfony文档中,您可以阅读该文档,以备不时之需:
类型:字符串默认值:标签是从字段名中“猜测”出来的
设置渲染字段时将使用的标签。设置为false将抑制标签。标签也可以直接设置在模板内部:
{{ form_label(form.name, 'Your name') }}
为什么不直接通过选择类型
选项来设置?您是否尝试了选项“choice_label”?@kingkero我按照对象“Symfony\Component\form\FormView”的错误-方法“name”中的建议,在细枝文件中尝试了{form_label(form.name,'Choose Event')}}在第行的default/index.html.twig中不存在6@LetsrocksSymfony.com文档中描述的choice_label选项表示它用于修改选项的显示名称。没有提到如何更改整个列表的标签。默认情况下,标签是从字段名称中“猜测”出来的。我按照symfony.com/doc/current/reference/forms/types/…中的建议,在小树枝文件中尝试了{form\u label(form.name,'Choose Event')}}。给出了错误-对象“symfony\Component\form\FormView”的方法“name”在default/index.html.twig第6I行添加了“label”=>“Choose Event”,进入控制器本身。现在一切正常。谢谢:)
{{ form_label(form.name, 'Your name') }}