I have a need to display a form to enter a blog post on the same page on which a news story is being displayed. The user is entering a blog post related to the story.
In my blog form I'm currently doing this to get the id of the story being displayed:
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();
}
Is there a cleaner way of doing this? It feels hacky to be taking the id of the notice (news story) from the request parameters.
UPDATE
I'm actually posting the form from a modal dialog via ajax and trying to maintain the notice_id value across requests. I am binding my parameters with the form before returning it to display errors:
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'));
}
}
}
I can't seem to get the notice_id to be bound with the form (it is a hidden field). The other values are binding fine.
I've also tried $form = new nbBlogPostForm(null,array('notice_id',$request->getPostParameter('nb_blog_post[notice_id]')));
FURTHER UPDATE
On the first pass through the form configure method relies on the notice_id being in the request, I think it was setting this to null when the form was created again via ajax. This fixes it:
$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());
}
If anyone has a cleaner way please let me know.