English 中文(简体)
Symfony2 窗体 - 已检查过所有复选框的实体字段
原标题:Symfony2 form - entity field with all checkboxes checked

我有一个没有课课的表格

class ProfilesSearchType extends AbstractType {

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add( disabilityType ,  entity , array(
                     class  =>  AldenXyzBundle:DisabilityType ,
                     property  =>  name ,
                     multiple  => true,
                     expanded  => true,
                ))
        ;
    }

调用控制器

public function listAction()
{
    $form = $this->createForm(
        new AldenXyzBundleFormTypeProfilesSearchType(), array());
    if (isset($_GET[ profile_search ]))
    {
        $form->bindRequest($request);
        $d = $form->getData();
        // some stuff here
    }
    return array(
         form  => $form->createView()
    );
}

How to set all checkboxes from disabilityType as checked by default? The class definition is (I deleted setters and getters)

class DisabilityType {

    /**
     * @var integer $disabilityTypeId
     *
     * @ORMColumn(name="disability_type_id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    private $disabilityTypeId;

    /**
     * @var string $name
     *
     * @ORMColumn(name="name", type="string", length=50, nullable=false)
     */
    private $name;

    /**
     * @var Profile
     *
     * @ORMManyToMany(targetEntity="Profile", mappedBy="disabilityType")
     */
    private $profile;

    public function __construct()
    {
        $this->profile = new DoctrineCommonCollectionsArrayCollection();
    }
}
最佳回答

我加进控制器

$disabilityDegree = $this->getDoctrine()->
    getRepository("AldenXyzBundle:DisabilityDegree")->findAll();
$form = $this->createForm(new AldenXyzBundleFormTypeProfilesSearchType(), array(
         disabilityType  => new DoctrineCommonCollectionsArrayCollection($disabilityType),
            )
    );
问题回答

您必须使用所有残疾类型来启动您的表格

您可通过以下方式完成此任务:

$disabilities = $this->getDoctrine()->getRepository("AldenXyzBundle:DisabilityType")-  >findAll();
$form = $this->createForm(new AldenXyzBundleFormTypeProfilesSearchType(), $disabilities);




相关问题
C# Form Problem: new form losing control and randomly hiding

I m encountering strange behavior with forms on a c# 3.5 app. On a button click, my form1 hides itself, creates a new form2, and shows form2. Form1 also contains the event method triggered when ...

TCPlistener.BeginAcceptSocket - async question

Some time ago I have payed to a programmer for doing multithread server. In the meantime I have learned C# a bit and now I think I can see the slowndown problem - I was told by that guy that nothing ...

RoR: before_save on nested object in form?

I have a form with a nested object (customer < order), and it works except that it keeps creating a new customer record. I d like to have it check to see if an existing customer is already ...

Receive POST from External Form

I have a form on another website (using a different backend) that I want to be able to POST to my Rails application (on a different domain). How do I generate a valid authenticity token for the ...

Getting posted values in MVC PartialView

I ve created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below): <% Html.RenderPartial("...

Recaptcha - Form Customization

Does anyone know if recaptcha can be fully customize without the default frame. I need the recaptcha image to only be a certain width as well as the input field. Has anyone done this before with ...

Better way to retain form data on manual back redirect?

I have a form that, on submit, requires the customer to look over the data, and then confirm the changes before saving the data. However, I m using a really rough way of retaining data if the user ...

热门标签