0

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 address?

Thank you

2
  • Possible duplicate: stackoverflow.com/questions/6345437/… Commented Jul 13, 2011 at 10:16
  • Not a dupplicate. I found a solution and posted the answer. :) Commented Jul 13, 2011 at 10:39

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Another (and better) way is to add a "request" option on your form. And I understand that you are using symfony Admin Generator. So you can use the moduleGeneratorConfiguration->getFormOptions() method ! This way you can inject any dependency to the Form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.