我想创建一个新表单,当我点击一个按钮
时直接在视图中显示它这是我的观点
我已经在控制器和我的新formType
中准备好了我的动作
class AttributOptionType extends AbstractType
{
private $type;
public function __construct($type){
$this->type = $type;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('translations', 'a2lix_translations', array(
'label' => 'formType.translations',
'fields' => array(
'name' => array(
'label' => 'Titre',
'field_type' => 'text',
'required' => false,
'attr' => array(
'class' => 'form-control input-circle-right'
)
),
'description' => array(
'label' => 'Titre',
'field_type' => 'text',
'required' => false,
'attr' => array(
'class' => 'form-control input-circle-right'
)
),
)
));
switch ($this->type) {
case 'radio' || 'checkbox':
$builder
->add('image', new MediaType(), array(
'label' => 'Image',
'attr' => array(
'class' => 'form-control input-circle-right',
),
'required' => false,
))
->add('code', 'text', array(
'label' => 'Image',
'attr' => array(
'class' => 'form-control input-circle-right',
),
'required' => false,
));
break;
case 'color':
$builder
->add('optionValue', 'text', array(
'label' => 'Image',
'attr' => array(
'class' => 'form-control input-circle-right',
),
'required' => false,
));
break;
}
}
我的行动:
public function createOptionAction($type){
$entity = new AttributOption();
$form = $this->createForm(new AttributOptionType($type), $entity);
}
切换$ type我将创建我的FormType,所以我想要做的就是直接使用AjaxRequest从视图创建此表单并在视图中显示我的新表单类型,
我该怎么做?
由于
答案 0 :(得分:0)
我更新了我的代码:
我的Ajax功能
<script type="text/javascript">
var id_select = $('#products_productsbundle_attribut_type').val();
$('.ajaxOption').on('click', function(event){
$.ajax({
type: "get",
data: {'id': id_select},
dataType: 'json',
url: "{{ path('admin_products_attribut_optionAdd') }}",
success: function( returnedData ) {
$( '.container' ).html( returnedData.form );
}
});
});
</script>
&#13;
我的行动:
public function createOptionAction(){
$entity = new AttributOption();
$form = $this->createForm(new AttributOptionType($_POST['id']), $entity);
$response = new JsonResponse(array(
'message' => 'Error',
'form' => $this->renderView('ProductsBundle:Administration:Attribut/form.html.twig',array(
'entity' => $entity,
'form' => $form->createView(),
)
)), 400);
return $response;
}
&#13;
这是我的路线:
admin_products_attribut_optionAdd: pattern:/ addOption 默认值:{_ control:&#34; ProductsBundle:BackEnd / Attribut:createOption&#34; }
错误是: 获取http://ispace/app_dev.php/admin/products/attribut/addOption?id=text 500(内部服务器错误)m.ajaxTransport.send @jquery.min.js:4m.extend.ajax @jquery.min.js:4(匿名函数)@new:1083m.event.dispatch @ jquery.min.js:3m.event.add.r.handle @jquery.min.js:3