开发者

Validating actions in Symfony (PHP)

I've been reading some Symfony documentation regarding validation/error-handling for controllers.

http://www.symfony-project.org/book/1_2/06-Inside-the-Controller-Layer

Validation and Error-Handling Methods (just over 2 thirds down)

I like the idea of a function automatically being called - validateMyAction before executeMyAction is called, and you can control which type of view is used, myActionSucess or myActionError for instance.

However, it doesn't seem to work with Symfony 1.4, I'm guessing this type of error handling/validation has been deprecated, so I'm not sure how I should be doing it with Symfony 1.4.

I know the pre and post Execute functions do something similar, but 开发者_JAVA技巧I prefer having a dedicated validation function for each action.

Or should I now be using Filters?

Thanks for any help/advice


That kind of validation worked in symfony 1.0 and 1.1, but is deprecated since 1.2, removed in 1.4. It has been replaced by the form framework, which offers much more. I recommend you read Chapter 10 (Forms) of the Gentle Introduction to symfony book. A dedicated book for the form framework also exists, but is incomplete - it still is a good reference for existing widgets and validators.


  public function preExecute()
  {
    $method = 'validate'.$this->getActionName();
    if (method_exists($this, $method))
    {
      if (!$this->{$method}())
      {
        return sfView::ERROR;
      }
    }
  }

Something like this could mimic a validator function for each action. But then my next question would be, what is the best way for displaying errors? Something like setting an error array in the validate function, then having a check in the template/view for the error array being set and then displaying the relevant information. This could then be expanded for warnings/notices.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜