2011-05-20 55 views
1

我需要显示表单以在显示新闻故事的同一页面上输入博客文章。用户正在输入与故事相关的博客文章。如何访问Symfony 1.4中显示的表单中的相关对象

在我的博客的形式,我现在这样做是为了让正在显示的故事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是很冒险的。

UPDATE

我是从一个模态对话框通过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再次创建时将其设置为空。这修复了它:

$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()); 

    } 

如果有人有更清洁的方式请让我知道。

回答

2

正确的方法是:

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

至于你提到的第一个参数必须是一个对象,以便空已定,那么第二个参数应该包含变量传递到窗体

UPDATE

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

创建表单内像这样的新方法:

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

然后调用它的操作中创建表格后:

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

的第二个是不要在显示表单时设置通知ID,而是按照前面提到的方式在创建操作中传递此类参数:

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

} 

然后,你必须使用你的表单中保存方法(在beggining),并将其分配给所需的字段是这样的:

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

的冷杉方法是清洁的,但是当你需要一些数据,后者是有用这与您的对象无关,例如,如果您需要将帖子保存在用户文件夹中,并且需要此用户文件夹的名称。

我希望这会有所帮助。

+0

感谢您的回复 - 我已经更新了一些更多的信息。 – codecowboy 2011-05-21 06:32:42

1

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

像这样:

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

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

问候。

+0

这看起来更干净 - 谢谢! – codecowboy 2011-05-20 12:31:10

+0

实际上表单只接受一个对象,而不是一个数组 – codecowboy 2011-05-20 15:45:29

+2

嗯, 实际上扩展sfForm类的窗体(最可能是你的窗体)接受三个参数: __construct($ defaults,$ options,$ CSRFSecret) 来自Symfony API:http://www.symfony-project.org/api/1_4/sfForm#method___construct – 2011-05-20 19:31:02

相关问题