来自GET的Symfony2.7实体类型 [英] Symfony2.7 entity type population from GET

查看:108
本文介绍了来自GET的Symfony2.7实体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过GET请求填充如下所示的实体类型设置?



当查询字符串包含正确的密钥对(?category = 232,查询字符串是表单提交的结果)时,实体类型不会填充 - 所以配对是正确的),但是如果我设置类型使用选择类型,它填充正确 - 我想保持为实体类型。

   - > add('category','entity',array(
'required'=> false,
'class'=>' AppBundle:FormElementOption',
'choices'=> $ this-> form_repository-> getOptionsForSelectEntities($ this-> directory-> getProject() - > getId()),
'choice_as_values'=> true,
'choice_label'=>'label',
'choice_value'=>'id',
'group_by'=& ){
return $ currentObject-> getFormElement() - > getLabel();
},
))

呈现的表单元素是:

 < select id =categoryname =categoryclass =form-control> 
< option value =>< / option>
< optgroup label =汽车品牌>
< option value =221> 6门< / option>
< option value =222>丰田< / option>
< option value =223> Jaguar< / option>
< option value =224> Skodai< / option>
< / optgroup>
< optgroup label =Car Doors Count>
< option value =226> 6门< / option>
< option value =227> Green< / option>
< / optgroup>
< optgroup label =Car Doors>
< option value =228> 1门< / option>
< option value =229> 2 Door< / option>
< option value =230> 3门< / option>
< option value =231> 4门< / option>
< option value =232> 6门< / option>
< / optgroup>
< / select>


解决方案

当然可以,但是您需要更多的配置外面的形式类型。这种方法对于过滤/搜索表单类型是完美的,但是为了避免资源/实体的创建。



无论何时创建一个FormType实例,你都可以给它一个名字,说搜索,表单将查询名为search [category]的参数的请求参数。



要使用无名表单类型,您应该在您的控制器中使用创建表单的替代语法,它是:

  $ form = $ this-> get(' form.factory') - > createNamed(null,new SearchType(),$ search); 

第一个 null 表示没有名称。 / p>

另外,请确保窗体选项 _method 设置为 get ,以便在查询字符串内搜索数据,而不是请求体(即 _POST )。


Can an entity type setup as shown below be populated via GET request?

Currently the entity type will not populate when the query string contains the correct key pair (?category=232, the query string is the result of the forms submission - so the pairing is correct), however if I setup the type to use a choice type it populates fine - I would like to keep this as an entity type.

->add('category', 'entity', array(
    'required' => false,
    'class' => 'AppBundle:FormElementOption',
    'choices' => $this->form_repository->getOptionsForSelectEntities($this->directory->getProject()->getId()),
    'choices_as_values' => true,
    'choice_label'=>'label',
    'choice_value'=>'id',
    'group_by' => function ($currentObject) {
        return $currentObject->getFormElement()->getLabel();
    },
))

The form element rendered is:

<select id="category" name="category" class="form-control">
    <option value=""></option>
    <optgroup label="Car Brand">
        <option value="221">6 Door</option>
        <option value="222">Toyota</option>
        <option value="223">Jaguar</option>
        <option value="224">Skodai</option>
    </optgroup>
    <optgroup label="Car Doors Count">
        <option value="226">6 Door</option>
        <option value="227">Green</option>
    </optgroup>
    <optgroup label="Car Doors">
        <option value="228">1 Door</option>
        <option value="229">2 Door</option>
        <option value="230">3 Door</option>
        <option value="231">4 Door</option>
        <option value="232">6 Door</option>
    </optgroup>
</select>

解决方案

Sure it can, but you need some more configuration "outside" of the form type. This method is perfect for "filter/search" form type, but is to be avoided for resource/entities creation.

Whenever you create a FormType instance you give it a name, say "search", the form will look into the request parameters for a parameter named 'search[category]'.

To use a "nameless" form type you should use in your controller an alternative syntax for creating the form, which is:

$form = $this->get('form.factory')->createNamed(null, new SearchType(), $search);

The first null means without name.

Also, make sure that the form option _method is set to get so that the data is searched inside the query string and not the request body (i.e. _POST).

这篇关于来自GET的Symfony2.7实体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆