Accessing sfWebRequest in Symfony Form Class?
I Currently writing a form for user to create a new record that require user's IP address.
I have this ready: $this->getObject()->setIp(ip2long($sf_request->getRequest()->getHttpHeader ('addr','remote')));
But it is obvious that I do not have the $sf_request object in my Form model. How can I access to this and get the user's IP addre开发者_如何学Pythonss?
Thank you
Try this:
Access to the $request parameter of the Action method and extract the ip from the http headers. Then, in the view file, try to send the variable as parameter to the form file. I don't know exactly how, but i know for example you can pass an object as a parameter to the form to fill it
Good luck
After a moment think about this. I move the setIP method to my action and override the default processForm method with this:
public function processForm(sfWebRequest $request, sfForm $form){
$form->getObject()->setIp(ip2long($request->getHttpHeader ('addr','remote')));
return parent::processForm($request, $form);
}
It works just fine.
精彩评论