首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >symfony setDefault不保存值

symfony setDefault不保存值
EN

Stack Overflow用户
提问于 2012-10-04 01:35:38
回答 1查看 329关注 0票数 0

我在操作中定义了一个表单,并将其传递给模板。保存时设置默认值失败。你能告诉我我哪里做错了吗?

在modules\question\actions中:

代码语言:javascript
复制
  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中:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-04 01:52:25

以下两个选项之一:

  1. 将setDefault代码放在QuestionForm类

  1. 将setDefault代码放在实例化form object

之后

常见的做法是选项#1,因为它会导致表单配置和操作之间的松散耦合。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12713847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档