我在自定义twig文件中的texarea字段时遇到问题 如果我自定义textarea,表单的验证不起作用 当我自定义textarea以外的其他字段并使用{{form_rest(form)}}时,验证工作正常。 如果我删除表达式if($ form-> isvalid()),持久性工作正常。 请帮我 这是我的代码
public function addAction()
{
$produit = new Produit();
$formBuilder = $this->createFormBuilder($produit);
$formBuilder->add('nom','text',array('required'=>false))
->add('prix','text')
->add('marque','text')
->add('description','textarea');
$form = $formBuilder->getForm();
return $this->render('zmsite1Bundle:Produit:add.html.twig', array( 'form' => $form->createView() ));
}
public function createAction()
{
$produit = new Produit();
$formBuilder = $this->createFormBuilder($produit);
$formBuilder->add('nom','text')
->add('prix','text')
->add('marque','text')
->add('description','textarea');
$form = $formBuilder->getForm();
$request = $this->getRequest();
if($request->getMethod() == 'POST'){
$form->bind($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($produit);
$em->flush();
return $this->redirect($this->generateUrl('produit_show',array('id' => $produit->getId())));
}
return $this->render("zmsite1Bundle:Produit:add.html.twig",array('form'=>$form->createView()));
}
}
<form method="post" {{ form_enctype(form) }} action="{{path('produit_create')}}">
<table>
<tr>
<td>{{form_label(form.nom,"Nom du produit")}}</td>
<td>{{form_widget(form.nom)}}</td>
<td>{{form_errors(form.nom)}}</td>
</tr>
<tr>
<td>{{form_label(form.marque,"Marque du produit")}}</td>
<td>{{form_widget(form.marque)}}</td>
<td>{{form_errors(form.marque)}}</td>
</tr>
<tr>
<td>{{form_label(form.prix,"Prix du produit")}}</td>
<td>{{form_widget(form.prix)}}</td>
<td>{{form_errors(form.prix)}}</td>
</tr>
<tr>
<td>{{textarea_label(form.description,"Description du produit")}}</td>
<td>{{textarea_widget(form.description)}}</td>
<td>{{textarea_errors(form.description)}}</td>
</tr>
</table>
<input type="submit" name="Valider" />
<?php
namespace zm\site1Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* produit
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="zm\site1Bundle\Entity\produitRepository")
*/
class produit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="marque", type="string", length=255)
*/
private $marque;
/**
* @var float
*
* @ORM\Column(name="prix", type="decimal")
*/
private $prix;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return produit
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set marque
*
* @param string $marque
* @return produit
*/
public function setMarque($marque)
{
$this->marque = $marque;
return $this;
}
/**
* Get marque
*
* @return string
*/
public function getMarque()
{
return $this->marque;
}
/**
* Set prix
*
* @param float $prix
* @return produit
*/
public function setPrix($prix)
{
$this->prix = $prix;
return $this;
}
/**
* Get prix
*
* @return float
*/
public function getPrix()
{
return $this->prix;
}
/**
* Set description
*
* @param string $description
* @return produit
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
答案 0 :(得分:0)
您的表单可能具有未呈现的csrf保护,因此导致表单无法完全验证。
像以前一样使用{{ form_rest(form) }}
(渲染隐藏的csrf标记)。或者在表单中将其关闭(我完全不确定这是否可以采用内联形式),否则它将采用您的表单类型(来自:http://symfony.com/doc/2.1/book/forms.html#csrf-protection)...
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\TaskBundle\Entity\Task',
'csrf_protection' => true,
'csrf_field_name' => '_token',
// a unique key to help generate the secret token
));
}