我在Symfony 3中使用表单时遇到一个奇怪的问题。
我的Assert在我的实体文件中不起作用。
在“开发者工具”中,它显示短语,但在视图中,所有内容都是空白的。
<?php
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table(name="users")
* @UniqueEntity("name")
* @UniqueEntity("email")
* @ORM\HasLifecycleCallbacks()
*/
class User implements UserInterface
{
/**
* @Assert\NotBlank()
* @Assert\NotBlank(message="Please enter your name.")
*/
private $name;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string", length=50, nullable=false)
*/
private $sername;
/**
* @var string
* @Assert\NotBlank()
* @Assert\Email()
*/
private $email;
/**
* @var string
*/
private $password;
/**
* @var integer
*/
private $role;
/**
* @var \DateTime
*/
private $created;
/**
* @var integer
*/
private $id;
/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set sername
*
* @param string $sername
*
* @return User
*/
public function setSername($sername)
{
$this->sername = $sername;
return $this;
}
/**
* Get sername
*
* @return string
*/
public function getSername()
{
return $this->sername;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set role
*
* @param integer $role
*
* @return User
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return integer
*/
public function getRole()
{
return $this->role;
}
/**
* Set created
*
* @param \DateTime $created
*
* @return User
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function getRoles()
{
return array($this->role);
}
public function getSalt()
{
return null;
}
public function eraseCredentials()
{
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return null;
}
}
我的表单操作(我将false设置为false以使用symfony有效选项):
public function addAction(Request $request){
$user = new User();
$form = $this->createFormBuilder($user)
->add('Name', TextType::class,array('required'=> false,'empty_data' => null))
->add('SerName', TextType::class,array('required'=> false,'empty_data' => null))
->add('role', TextType::class,array('required'=> false,'empty_data' => null))
->add('email', EmailType::class,array('required'=> false,'empty_data' => null))
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'first_name' => 'pass',
'second_name' => 'confirm',
'required' => false,
))
->add('save', SubmitType::class, array('label' => 'Create Post'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$user = new Users();
// $form->getData() holds the submitted values
// but, the original `$task` variable has also been updated
$password = $form->get('password')->getData();
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user,$password);
$user->setPassword($encoded);
$user = $form->getData();
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('test_homepage');
}
//return $this->render('UserBundle:Default:add.html.twig');
return $this->render("TestBundle:Default:add.html.twig",array("form"=>$form->createView()));
}
编辑(I PUT VIEW FILE): 我的观点:
{%extends "principal.html.twig"%}
{% block body %}
{{parent()}}
<h1> {% trans %}New User{% endtrans %}</h1>
{{ form_start(form,{'attr':{'novalidate':'novalidate','role':'form'}}) }}
{{ form_label(form.Name,'Username', {'label_attr': {'class': 'foo'}}) }}
{{ form_widget(form.Name,{'attr':{'class':'form-control'}}) }}
{{form_errors(form.Name)}}
{{ form_label(form.SerName,'Last name', {'label_attr': {'class': 'foo'}}) }}
{{ form_errors(form.SerName) }}
{{ form_widget(form.SerName,{'attr':{'class':'form-control'}}) }}
{{ form_label(form.role,'Role', {'label_attr': {'class': 'foo'}}) }}
{{ form_errors(form.role) }}
{{ form_widget(form.role,{'attr':{'class':'form-control'}}) }}
{{ form_label(form.email,'Email', {'label_attr': {'class': 'foo'}}) }}
{{ form_widget(form.email, {'attr':{'class':'form-control'}}) }}
{{ form_errors(form.email) }}
{{ form_label(form.password.pass, "Password", {'attr':{'class':'form-control'}}) }}
{{ form_errors(form.password) }}
{{ form_widget(form.password.pass, {'attr':{'class':'form-control'}}) }}
{{ form_label(form.password.confirm, "Confirm" , {'attr':{'class':'form-control'}}) }}
{{ form_widget(form.password.confirm, {'attr':{'class':'form-control'}}) }}
{{ form_widget(form.save, { 'label': 'Create User','attr':{'class':'btn btn-primary'}}) }}
{{ form_end(form) }}
{% endblock %}
答案 0 :(得分:1)
你应该阅读doc about form rendering 在twig模板中,您必须使用
显示错误{{ form_errors(form.age) }}
或整行(标签,错误信息,inpout)和
{{ form_row(form.age) }}
几乎等于
<div>
{{ form_label(form.age) }}
{{ form_errors(form.age) }}
{{ form_widget(form.age) }}
</div>
在您的情况下,这意味着您必须以这种方式添加form_errors:
{{ form_label(form.Name,'Username', {'label_attr': {'class': 'foo'}}) }}
{{ form_widget(form.Name,{'attr':{'class':'form-control'}}) }}
{{ form_errors(form.Name) }}