Symfony1 symfony框架

Symfony1 symfony框架,symfony1,validation,Symfony1,Validation,我在actions.class.php中使用此验证代码来验证输入 $name = $this->getRequestParameter('title'); if (!$name) { $this->getRequest()->setError('The name field cannot be left blank'); return false; } 但是遇到这样的错误 You must set "compat_10" to true if you

我在actions.class.php中使用此验证代码来验证输入

$name = $this->getRequestParameter('title');    
if (!$name)
{
    $this->getRequest()->setError('The name field cannot be left blank');
    return false;
}
但是遇到这样的错误

You must set "compat_10" to true if you want to use this method which is deprecated.

在错误日志中。收到此错误后,我在settings.yml中将
compat_10
设置为
true
,但仍会收到相同的错误。

根据您希望失败的程度,您可以执行以下操作之一:

  • 抛出一个异常。。。这显示了通用500错误消息
  • 转到404。例如,当标题是url的一部分或其他类型的选择参数时。有一种简便的方法可以做到这一点:
    $this->forward404($name,'未提供标题')
  • 将消息设置为模板var
    $this->errorMessage='…'
    然后返回错误视图
    return sfView::error
    ,并在模板
    actionError.php
    中显示一条漂亮的消息

(我可能会说第二个)

您可以使用异常处理进行错误处理

try {
 $this->getRequest()
 } catch (\Doctrine\ORM\NoResultException $e) {
 return null;
}

$this->getRequest()->setError()
方法仅在symfony 1.0和1.1中可用,它与symfony 1.2相比,验证程序行为和操作中的所有错误处理都被删除,以便更符合新的表单框架

我猜您使用的是1.2到1.4版本,在这种情况下,您尝试使用的方法已被弃用,您必须为此启用compat_10插件。我看到您已经启用了它,但由于它的行为来自于插件,因此您必须确保在
ProjectConfiguration::configure()
方法中启用了
sfCompat10Plugin


作为补充说明,对于此类错误,最好使用验证程序形成表单框架。

您没有使用表单框架吗?您可以使用它在表单字段上设置验证器