symfony バリデーションエラー csrf token: Required.

2011年7月24日 Posted by PURGE

下記エラー発生。3日程ハマった。

csrf token: Required.

基本的には、バインド (sfForm::bind) の使用の仕方を理解していなかった。
formクラスに下記の要素名定義を行っていなかった為にバインドがうまく行かずに、バリデーションエラーとなっていた。

アクションクラス

class HogeAction extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //フォーム生成
    $this->form = new HogeForm();
    if($request->isMethod(sfRequest::POST))
    {
      $this->form->bind($request->getParameter('hoge'));
      if($this->form->isValid())
      {
        $hoges = $this->form->getValues();
        $hoge = $hoges['hoge'];
        //
        $this->getUser()->setFlash('hoge', $hoge);
        $this->redirect('next/foo');
    }
  }
}

フォームクラス

class HogeForm extends BaseForm
{
  public function configure()
  {
    //フォーム設定
    $this->setWidgets(array('hoge'=> new sfWidgetFormInput()));
    //バリデーション設定
    $this->setValidators(array('hoge'=> new sfValidatorString()));
    //要素名定義
    $this->widgetSchema->setNameFormat('hoge[%s]');
  }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です