在Symfony2中使用表單有一個奇怪的問題。Symfony2表單驗證總是返回錯誤:該值不能爲空
我第一次添加的驗證作爲annotations
Job
實體類裏面在這裏:
class Job
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\NotBlank()
* @Assert\Choice(callback="getTypeValues")
*/
protected $type;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $company;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $logo;
/**
* @Assert\Image()
*/
protected $file;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Url()
*/
protected $url;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $position;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
protected $location;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $description;
/**
* @ORM\Column(type="text")
* @Assert\NotBlank()
*/
protected $how_to_apply;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
*/
protected $token;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $is_public;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $is_activated;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;
/**
* @ORM\Column(type="datetime")
*/
protected $expires_at;
/**
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
protected $updated_at;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="jobs")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $category;
}
我創建了一個JobType
類,並用它一個表單中。所以我可以添加工作。
class JobType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type', 'choice', array('choices' => Job::getTypes(), 'expanded' => true))
->add('category')
->add('company')
->add('file', 'file', array('label' => 'Company logo', 'required' => false))
->add('url')
->add('position')
->add('location')
->add('description')
->add('how_to_apply', null, array('label' => 'How to apply?'))
->add('is_public', null, array('label' => 'Public?'))
->add('email')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Ibw\JobeetBundle\Entity\Job',
));
}
public function getName()
{
return 'job';
}
}
這裏是我的控制器:
public function createAction(Request $request)
{
$entity = new Job();
$form = $this->createForm(new JobType(), $entity);
$form->handleRequest($request);
if($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('ibw_job_preview', array(
'company' => $entity->getCompanySlug(),
'location' => $entity->getLocationSlug(),
'position' => $entity->getPositionSlug(),
'token' => $entity->getToken(),
)));
} else {
return new Response(var_dump($form->getErrorsAsString()));
// return new Response($form->getErrorsAsString());
// return $this->render('IbwJobeetBundle:Job:new.html.twig', array(
// 'form' => $form->createView(),
// ));
}
}
現在,當我做var_dump($form->getErrorsAsString())
我得到:
string 'ERROR: This value should not be blank.
type:
0:
No errors
1:
No errors
2:
No errors
category:
No errors
company:
No errors
file:
No errors
url:
No errors
position:
No errors
location:
No errors
description:
No errors
how_to_apply:
No errors
is_public:
No errors
email:
No errors
' (length=355)
或當我做var_dump($form->getErrors())
我得到:
array (size=1)
0 =>
object(Symfony\Component\Form\FormError)[614]
private 'message' => string 'This value should not be blank.' (length=31)
protected 'messageTemplate' => string 'This value should not be blank.' (length=31)
protected 'messageParameters' =>
array (size=0)
empty
protected 'messagePluralization' => null
我有不知道什麼克嵌入此ERROR: This value should not be blank.
錯誤。我很難搞清楚。任何幫助將不勝感激。
當窗體應該顯示在視圖中,或者當您將窗體發佈到控制器以處理窗體時,您是否在GET請求中收到這些錯誤? –
只是一個想法...我認爲你應該@Assert \ NotBlank()或使用回調函數 - 不是兩個。 – stwe
@KenHannel當POST控制器處理髮布的表單時會發生這種情況。所以是在POST請求。 –