1

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 ?

4 Answers 4

5

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.

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

6 Comments

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.
When you call $this->getOption('parameter') within the form's configure method, what does it return?
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.
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 tried to get the option with $this->getOption('parameter') and didn't work. Looks like the right way to do it is get it from the Form instance like this: $this->form->getOption('parameter')
|
0

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 }

Comments

0

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.

Comments

0

If the parameter is an attribute of the object, you can do :

$object = new Class();
$object->setAttribute($request->getParameter('myParam'));

$form = new ClassForm($object);

1 Comment

Or if you need a form option $form = new ClassForm($object, array("myoption" => $request->getParameter("myOption")));

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.