简体   繁体   English

Symfony2 Ajax + Forms

[英]Symfony2 Ajax + Forms

I use Symfony 2.6 (I know, deprecated soon). 我使用Symfony 2.6(我知道,很快就会弃用)。 I want to create a web page that contains the same form 5 or 6 times (to create User) because one User can create limited numbers of others Users. 我想创建一个包含5到6次相同表单的网页(创建用户),因为一个用户可以创建数量有限的其他用户。 So I can't use FormBuilder because you can't display several times. 因此,我无法使用FormBuilder,因为您无法显示多次。 Then I created normals HTML Forms: 然后,我创建了法线HTML表单:

 <form class="fcrv" action="{{ path('path') }}" method="POST">
      <input type="text" name="Name" placeholder="Name"/>
      <textarea rows="4" cols="50" name="comm"></textarea> 
      <input type="submit" value="Validate">
 </form>

I Handle Form by Form with Ajax and I sent the data needed in my Controller. 我使用Ajax逐个处理表单,并在Controller中发送了所需的数据。 I can get data in Controller with : 我可以使用以下命令在Controller中获取数据:

$name = $request->request->get('Name');
$comm = $request->request->get('comm');

It's work. 是工作。 After this I want to Simulate the behavior of a normal Form. 在此之后,我想模拟普通表格的行为。 I need to create my new Form and put the data in, so I did : 我需要创建我的新表单并将数据放入,所以我做了:

//create a new User
$user = new User();
$user->setCreateDate(new Date());

//Create the form
$form = $this->createForm(new UserType(), $user);
$form->setData(array('name'=>$name));
$form->setData(array('comm'=>$comm));

$form->submit($request->request->get($form->getName()));

if ($form->isValid()) {
   //Some actions
}

My UserType : 我的用户类型

<?php

 namespace Test\Bundle\Form;

 use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;

 class UserType extends AbstractType
 {
     /**
      * @param FormBuilderInterface $builder
      * @param array $options
      */
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
         $builder
             ->add('name', 'text')
             ->add('comm', 'text')
         ;
     }

     /**
      * @param OptionsResolverInterface $resolver
      */
     public function setDefaultOptions(OptionsResolverInterface $resolver)
     {
         $resolver->setDefaults(array(
             'data_class' => 'Cfau\CleBundle\Entity\FraisVisite',
             'csrf_protection' => false
         ));
     }

     /**
      * @return string
      */
     public function getName()
     {
         return 'test_bundle_user';
     }
}

And the conditions in entity User is just : @Assert\\NotBlank() 而实体User中的条件就是: @Assert \\ NotBlank()


But the form isn't valid ! 但表格无效!

I disable CSRF token. 我禁用CSRF令牌。

I use 我用

 echo $form->getErrors(true, false);

to see what wrongs with my form and it say : name: ERROR: This value should not be blank. 看看我的表单有什么错误并显示: 名称:错误:此值不应为空。 But I put data with $form->setData(array('name'=>$name)); 但是我用$ form-> setData(array('name'=> $ name)); !

Can you help me. 你能帮助我吗。 Thanks ! 谢谢 !

If I understood correctly what you are trying to do, I think you should setData after submit , instead of before. 如果我理解正确的是你正在尝试做的,我想你应该setDatasubmit ,而不是之前。

Also, be careful since you seem to use "name" in your FormBuilder but "Name" in your view (so maybe in your AJAX call as well). 此外,因为你似乎使用要小心"name"在你的FormBuilder但"Name" (在你的AJAX调用,所以也许也一样)在您的视图。 This might be the problem. 这可能是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM