ニュース記事が表示されているのと同じページにブログ投稿を入力するためのフォームを表示する必要があります。ユーザーは、ストーリーに関連するブログ投稿を入力しています。
私のブログフォームでは、現在、表示されているストーリーの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'));
}
}
}
notify_idをフォームにバインドすることができないようです(これは非表示のフィールドです)。他の値は正常にバインドされています。
I've also tried $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('nb_blog_post[notice_id]')));
さらなるアップデート
フォームの最初のパスでconfigureメソッドは、リクエストに含まれる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());
}
誰かがよりクリーンな方法を持っているなら、私に知らせてください。