我正在尝试运行一个简单的表单,但它不起作用且没有错误消息
UserType.php
<?php
namespace BISSAP\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Routing\Router;
class UserType extends AbstractType
{
/**
* @var Router
*/
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('required' => false))
->setAction($this->router->generate('bissap_forum_test'))
->setMethod('POST')
->add('Envoyer', 'submit', array(
'attr' => array(
'class' => 'btn right-flt'
)));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BISSAP\UserBundle\Entity\Usertmp'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array( 'data_class' => $this->class, 'intention' => 'Registration', 'validation_groups' => array('Registration')));
}
/**
* @return string
*/
public function getName()
{
return 'userType';
// return 'bissap_forumbundle_user';
}
}
TController.php
<?php
namespace BISSAP\ForumBundle\Controller;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use BISSAP\ForumBundle\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use BISSAP\UserBundle\Entity\Usertmp;
use Symfony\Component\HttpFoundation\Request;
class TController extends Controller
{
public function indexAction(Request $request)
{
//$entity = new Usertmp();
$form = $this->createForm('userType', $entity, array('validation_groups' => 'Registration'));
$data_post = $request->request->get('name');
if ($form->isValid())
{
return $this->redirectToRoute('bissap_forum_index');
}
return $this->render('BISSAPForumBundle:T:index.html.twig', array('form'=> $form->createView(), 'errors_tmp' => $this->getErrorMessages($form), 'data_post' => $data_post2));
}
private function getErrorMessages(\Symfony\Component\Form\Form $form)
{
$errors = array();
foreach ($form->getErrors(true, false) as $error) {
$errors[] = $error->current()->getMessage();
}
return $errors;
}
}
?>
在此Controller中,我尝试获取POST数据并将其显示在模板(index.html.twig)中:
$data_post = $request->request->get('name');
但$data_post
为空
UserTmp.php
<?php
namespace BISSAP\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* BISSAP\UserBundle\Entity\User
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="BISSAP\UserBundle\Entity\UserRepository")
*/
class Usertmp
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Usertmp
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
service.yml
bissap_forum.test_form:
class: BISSAP\ForumBundle\Form\UserType
arguments: ['@router']
tags:
- { name: form.type, alias: userType }
index.html.twig
{{form( form )}}
{% for error in errors_tmp %}
<div>error : {{ error }}</div>
{% endfor %}
<div>data_post : {{ data_post }}</div>
使用$form->createView()
生成的表单HTML:
<form name="userType" method="post" action="/bodykoncept/web/app_dev.php/forum/test">
<div id="userType"><div>
<label for="userType_name">Name</label>
<input id="userType_name" name="userType[name]" type="text">
</div>
<div>
<button type="submit" id="userType_Envoyer" name="userType[Envoyer]" class="btn right-flt">Envoyer</button>
</div>
<input id="userType__token" name="userType[_token]" value="ldFnSgioYkQEI6WOzqFJterUSrBoEagSct2m5p2oQmY" type="hidden">
</div>
</form>
<div>data_post : </div>
<input id="userType_name" name="userType[name]" type="text">
:
名称输入= 用户名[名称] 也许是因为这个原因
TController.php 中的 $data_post = $request->request->get('name');
为空。
此表单未在$this->submitted is false
vendor/symfony/symfony/src/Symfony/Component/Form.php