如何更改choiceType输入字段的标签?

时间:2016-04-19 11:22:03

标签: symfony doctrine-orm

我正在研究symfony2.8并尝试根据需要显示choiceType(radiobutton)的Label(不是用于存储输入数据的变量,例如eT1或eT2的doctrine猜测的那个)。是否有语法或我是否需要将变量名称更改为我想要显示的名称,例如。 event1或event2。此外,如果我想显示“请选择时段1的事件”而不仅仅是事件1?

<?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(),
            ));
        }
    }

以下是事件实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * events
 *
 * @ORM\Table(name="events")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\eventsRepository")
 */
class events {

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var int
     *
     * @ORM\Column(name="user_id", type="integer")
     */
    protected $user_id;

    /**
     * @var int
     *
     * @ORM\Column(name="ET1", type="integer")
     */
    protected $eT1;

    /**
     * @var int
     *
     * @ORM\Column(name="ET2", type="integer")
     */
    protected $eT2;

    /**
     * @ORM\ManyToOne(targetEntity="users", inversedBy="multievents")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $singleUser;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * Get user_id
     *
     * @return integer 
     */
    public function getuser_id() {
        return $this->user_id;
    }

    /**
     * Set user
     *
     * @param integer $user
     * @return events
     */
    public function setUser($user) {
        $this->singleUser = $user;
    }


    /**
     * Set eT1
     *
     * @param integer $eT1
     * @return events
     */
    public function setET1($eT1) {
        $this->eT1 = $eT1;

        return $this;
    }

    /**
     * Get eT1
     *
     * @return integer 
     */
    public function getET1() {
        return $this->eT1;
    }

    /**
     * Set eT2
     *
     * @param integer $eT2
     * @return events
     */
    public function setET2($eT2) {
        $this->eT2 = $eT2;

        return $this;
    }

    /**
     * Get eT2
     *
     * @return integer 
     */
    public function getET2() {
        return $this->eT2;
    }
}

1 个答案:

答案 0 :(得分:0)

在symfony文档之后,您可以阅读HERE,以防万一:

类型字符串 默认:标签是从 字段名称“猜到” < /强>

设置渲染字段时将使用的标签。设置为false将禁止标签。标签也可以直接在模板中设置:

{{ form_label(form.name, 'Your name') }}