Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony2 isValid()方法始终返回true_Php_Mysql_Validation_Symfony - Fatal编程技术网

Php symfony2 isValid()方法始终返回true

Php symfony2 isValid()方法始终返回true,php,mysql,validation,symfony,Php,Mysql,Validation,Symfony,我在将数据保存到数据库之前验证数据,但到目前为止,当javascript验证关闭时,我无法检测对象是否有效。我是这样做的: $editForm = $this->createEditForm($post, $category_id, $section_id, $topic_id); $editForm->handleRequest($request); if ($editForm->isValid()) { $em->flush();

我在将数据保存到数据库之前验证数据,但到目前为止,当javascript验证关闭时,我无法检测对象是否有效。我是这样做的:

$editForm = $this->createEditForm($post, $category_id, $section_id, $topic_id);
      $editForm->handleRequest($request);

if ($editForm->isValid()) {
          $em->flush();
          return $this->redirect($this->generateUrl('topic_show', array('category_id'=> $category_id, 'section_id'=> $section_id, 'id'=> $topic_id)));
      }
不幸的是,方法isValid()每次都返回true,然后我从mysql中得到错误

我尝试了不同的方法,从

但它也不起作用

更新

导致错误的“内容”属性

/**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;
您必须在
实体中设置
,也可以在
表单类型中设置
来为字段设置验证规则

这里有一个例子

use Symfony\Component\Validator\Constraints as Assert;

...

 /**
  * @var string
  *
  * @ORM\Column(name="content", type="text")
  * @Assert\NotBlank(
  *      message = "Your error message here if content is empty"      
  * )   
  */
 private $content;

您的
实体
表单类型
中是否有任何属性?不,在symfony中,任何属性都是defaultNo所必需的。您必须使用或使字段成为必需字段。但我收到mysql错误,告诉我“content”列不能为null,并且我的模型中没有Assert。我将更新我的问题还请注意,将必需选项设置为true不会导致应用服务器端验证。但是我不想让内容为空:)我想验证它是否为空,然后添加约束。知道以后如何显示此消息吗?通过使用
{form_errors(form)}
-好的,但是如果表单无效,我将重定向到编辑页面,我是否应该以参数形式发送它?
use Symfony\Component\Validator\Constraints as Assert;

...

 /**
  * @var string
  *
  * @ORM\Column(name="content", type="text")
  * @Assert\NotBlank(
  *      message = "Your error message here if content is empty"      
  * )   
  */
 private $content;