0

アクションでフォームを定義し、それをテンプレートに渡します。保存時にデフォルト値を設定できません。私が間違っていることを教えてもらえますか?

modules \ question \ actions:

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new questionForm();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      // create question
      $user = $this->getUser()->getGuardUser()->getId();

      $question = new Question();
      $question->setQuestionTitle($this->getRequestParameter('question_title'));
      $question->setQuestionBody($this->getRequestParameter('question_body'));
      $question->setUserId($user);
      $question->save();

      $question->addTagsForUser($this->getRequestParameter('tag'), $user);

      return $this->redirect('question/index');
    }

    //set current user as default
    $this->form->setDefault('user_id',$this->getUser()->getGuardUser()->getId());
  }

\ modules \ question \ templates:

<?php echo $form->renderFormTag('question/new') ?>
  <table>
    <?php echo $form['question_title']->renderRow(array('size' => 40), 'Question title:') ?>
    <?php echo $form['question_body']->renderRow(array('cols' => 40, 'rows' => 10), 'Description:') ?>

    <?php echo input_auto_complete_tag('tag', '', '@tag_autocomplete', 'autocomplete=off', 'use_style=true') ?>
    <tr>
      <td colspan="2">
        <input type="submit" value="ask it" />
      </td>
    </tr>
  </table>
</form>
4

1 に答える 1

0

2つのオプションのいずれか:

  1. setDefaultコードをQuestionFormクラスに配置します

また

  1. フォームオブジェクトをインスタンス化した直後にsetDefaultコードを配置します

一般的な方法はオプション#1です。これは、フォーム構成とアクションの間の結合が緩くなるためです。

于 2012-10-03T17:52:25.257 に答える