I have issue with Assert/NotBlank(). It just doesn't work. Every time when I send a blank form, I see 500, SQLSTATE[23000]: Integrity constraint violation: 1048 Column '***' cannot be null
instead of some error.
我对Assert/NotBlank()有异议。它只是不工作。每次当我发送一个空白表单时,我看到500,SQLSTATE[23000]:完整性约束违反:1048列'***'不能为空,而不是某些错误。
I have this in entity: use Symfony\Component\Validator\Constraints as Assert;
我有这个实体:使用Symfony\组件\Validator\约束作为断言;
...
/**
* @var string
*
* @ORM\Column(name="text", type="text")
* @Assert\NotBlank()
*/
private $text;
...
EXTRA: Controller: Controller, for adding ads to the database.
控制器:控制器,用于向数据库添加广告。
public function insertAdAction($categoryId, Request $request)
{
$em = $this->getDoctrine()->getManager();
$category = $em->getRepository('DbBundle:Category')->findAll();
$ads = new Ads();
$form = $this->createForm(new InsertAdType(), $ads, array(
'categoryId' => $categoryId,
'name' => $request->cookies->get('name'),
'phone' => $request->cookies->get('phone'),
'email' => $request->cookies->get('email'),
'password' => $request->cookies->get('password'),
));
$form->handleRequest($request);
if ($form->isValid()) {
try {
$user = new User();
$validator = $this->get('validator');
$validator->validate($ads);
$byCategory = $em->getRepository('DbBundle:Category')->findOneBy(array('id' => $categoryId), array());
$categoryName = $byCategory->getName();
$phone = $form['phone']->getData();
$ads->setCategory($categoryName);
$ads->setCategoryUrl($this->get('WebPortalBundle')->seoUrl($categoryName));
$ads->setCountry($form['country']->getData());
$ads->setText(nl2br($form['text']->getData()));
$ads->setTitleUrl($this->get('WebPortalBundle')->seoUrl($form['title']->getData()));
$ads->setSubcategoryUrl($this->get('WebPortalBundle')->seoUrl($form['subcategory']->getData()));
$ads->setIp($request->getClientIp());
if (!$em->getRepository('DbBundle:User')->findOneBy(array('phone' => $form['phone']->getData()))) {
$user->setPhone($phone);
$em->persist($user);
}
$ads->setPath(implode(',', $this->uploadImg($form['file']->getData())));
$em->persist($ads);
$em->flush();
$response = new Response();
$time = time() + 31536000;
$response->headers->setCookie(new Cookie('name', $form['name']->getData(), $time));
$response->headers->setCookie(new Cookie('email', $form['email']->getData(), $time));
$response->headers->setCookie(new Cookie('phone', $form['phone']->getData(), $time));
$response->headers->setCookie(new Cookie('password', $form['password']->getData(), $time));
$response->send();
} catch (Exception $ex) {
throw new \ErrorException;
}
}
return $this->render('WebPortalBundle:Default/InsertAd:insertAd.html.twig', array('category' => $category, 'insertForm' => $form->createView()), $this->get('WebPortalBundle')->cache($request));
}
What do I do wrong?
我做错了什么?
1 个解决方案
#1
0
I bet that you have no setDefaultOptions
method in your form type.
我打赌您的表单类型中没有setDefaultOptions方法。
use Symfony\Component\OptionsResolver\OptionsResolver;
...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DBBundle\Entity\Ads',
));
}
#1
0
I bet that you have no setDefaultOptions
method in your form type.
我打赌您的表单类型中没有setDefaultOptions方法。
use Symfony\Component\OptionsResolver\OptionsResolver;
...
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DBBundle\Entity\Ads',
));
}