views:

161

answers:

3

Hello,

I have a form, and I want to pass it one parameter which must use to fill a widget.

I pass the parameter in my url :

url_for('myModule/new?parameter='.$myParam)

I have tried to take the parameter in my action and display it in my tamplate, but it doesn't work.

Action :

$this->param = $request->getParameter('parameter');

Template :

echo param;

But I can't recover it in my form.

How can I do this ?

A: 

Hi,

you have to define a route for this URL in your routing.yml and add your parameter to this route.

for example:

mymoduel_new:
   url:    myModule/new/:paramter/
   param:  { module: myModule, action: new }
funkdoobiest
+1  A: 

If this parameter is needed for initializing your form then you could do it like this (keep in mind that you should always validate user input)

  public function executeNew($request)
  {
    $formOptions = array('parameter' => $request->getParameter('parameter'));
    $this->form = new MyForm(array(), $formOptions);
    // Then within your form you access it with:
    // $parameter = $this->options['parameter'];
    // or even better:
    // $parameter = $this->getOption('parameter');
    ... more code ...
  }

If the parameter is submitted as part of the form then you should bind it like this:

  public function executeNew($request)
  {
    $this->form = new MyForm();
    if ( $request->isMethod('post') ) {
      $this->form->bind($request->getPostParameters());
    }
    ... more code ...
  }

Please refer to the Symfony's Forms in Action for more on how to create and use forms.

Edit: Added $this->getOption('parameter') to the code example.

lunohodov
I have tried your first solution with $formOptions. But xDebug call me : 'parameter' => null. However I have passed something in my URL. I don't understand why.
Corum
When you call $this->getOption('parameter') within the form's configure method, what does it return?
lunohodov
I have an error : "Call to undefined method sfPartialView::getOption". The problem doesn't comme from the method to recover the value, the problem is the parameter isn't passed to the template.
Corum
It seems to me that I misunderstood your question. Do you use the Symfony Forms framework or with 'form' you meant plain HTML forms?
lunohodov
I think I use symfony forms framework...
Corum
A: 

You need to pass it to the form constructor, both the constructors of sfForm and sf{Propel,Doctrine}Form take a parameter called $options, use that. Store the value in a private property, so you can use in in the form's configure method.

Maerlyn