1

我需要显示一个表单以在显示新闻故事的同一页面上输入博客文章。用户正在输入与故事相关的博客文章。

在我的博客表单中,我目前正在这样做以获取正在显示的故事的 ID:

public function configure()
{
    $this->setWidget('image_filename', new sfWidgetFormInputFileEditable(array(
    'file_src'    => '/uploads/blogpost_images/thumbnails/thumb_'.$this->getObject()->image_filename, //displayed for existing photos
    'edit_mode'   => !$this->isNew(),
    'is_image'    => true,
    'with_delete' => false,

    )));

    $this->setValidator('image_filename', new sfValidatorFile(array(
      'mime_types' => 'web_images',
      'required' => $this->isNew(),
      'path' => sfConfig::get('sf_upload_dir').'/blogpost_images',
      'validated_file_class' => 'BlogPostValidatedFile',
    )));
    $this->setValidator('url', new sfValidatorUrl(array('required' => true)));
    $this->setValidator('title', new sfValidatorString(array('required' => true)));

    $this->setWidget('user_id', new sfWidgetFormInputHidden(array(),array(
        'value'=>sfContext::getInstance()->getUser()->getId())
            ));

    // get the request params to access the notice ID and pass it back to the form for
    // saving with the blog post

    $params = sfContext::getInstance()->getRequest()->getParameterHolder();

    $this->setWidget('notice_id', 
            new sfWidgetFormInputHidden(array(),array(
                'value'=>$params->get('id'))
                    ));

    $this->removeFields();


}

有更清洁的方法吗?从请求参数中获取通知(新闻故事)的 id 感觉很奇怪。

更新

我实际上是通过 ajax 从模态对话框中发布表单,并尝试在请求之间维护 notice_id 值。在返回表单以显示错误之前,我将参数与表单绑定:

  public function executeModal(sfWebRequest $request) {

  if ($request->isXmlHttpRequest()) {
    //return $this->renderText('test'.$request->getParameterHolder()->getAll());
    $params = $request->getParameter('nb_blog_post');
    $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('notice_id')));

    $form->bind($params,$request->getFiles());


    if ($form->isValid()) {
      $nb_blog_post = $form->save();
      $this->getUser()->setFlash('notice', 'Your blog post was successfully created');
      $this->redirect('@noticeboard');

    } else {
      return $this->renderPartial('form',array('form'=>$form,'form_id'=>'blogpost'));
    }
  }


  }

我似乎无法让 notice_id 与表单绑定(它是一个隐藏字段)。其他值绑定良好。

I've also tried $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('nb_blog_post[notice_id]')));

进一步更新

在第一次通过表单配置方法时,依赖于请求中的 notice_id,我认为当通过 ajax 再次创建表单时将其设置为 null。这修复了它:

   $params = sfContext::getInstance()->getRequest()->getParameterHolder();
    if (($params->get('id'))) {
    $this->setWidget('notice_id', 
            new sfWidgetFormInputHidden(array(),array(
                'value'=>$params->get('id'))
                    ));
    } else {
          $this->setWidget('notice_id', 
            new sfWidgetFormInputHidden());

    }

如果有人有更清洁的方法,请告诉我。

4

2 回答 2

2

正确的方法是:

$form = new YourForm(null,array('notice_id' => $notice_id));

正如您提到的,第一个参数必须是一个对象,所以给出 null,第二个参数应该包含要传递给表单的变量

更新

随着提供的新信息。你有两个选择,第一个:

在表单中创建一个新方法,如下所示:

public function setNoticeId($id){
$this->getWidget('notice_id')->setAttribute('value'),$id);
}

然后在您的操作中创建表单后调用它:

$this->form = new Form();
$this->form->setNoticeId($id);

第二个是在显示表单时不设置通知ID,而是按照我之前提到的方式在create action中传递这样的参数:

public function executeCreate(sfWebRequest $request)
{
$this->form = new Form(null, array('notice_id'=>$id));
$this->processForm($request,$this->form);

}

然后您必须在表单保存方法中使用它(在开始时)并将其分配给所需的字段,如下所示:

$this->values['notice_id'] = $this->getOption('notice_id');

第一种方法更简洁,但后者在您需要一些与您的对象无关的数据时很有用,例如,如果您需要将图像与您的帖子一起保存在用户文件夹中并且您需要此类用户文件夹的名称。

我希望这有帮助。

于 2011-05-20T17:11:38.727 回答
1

我认为最简单方便的方法是在创建新表单对象时传递表单元素的默认值。

像这样:

$form = new YourForm(array('notice_id' => $notice_id));

这将自动为您的表单元素设置值。(也适用于所有表单元素。)

问候。

于 2011-05-20T12:18:13.757 回答