1
votes

I have a variable that need to be bind into a form in symfony, how can I bind data from action file to form?

The Action File $this->unitCost = $this->getUser()->getAttribute('unit_cost'); $value_lists = ($this->unitCost);

    foreach($value_lists as $values)
        {
        echo $values['unit_cost'];
        }
    $form = new MyForm(); $form->bind(array('unit_cost' => $values['unit_cost']));
     //print_r($value_lists);

Form public function configure() {

    $cost_range[''] = '-- Please select --';
    for ($i = 0; ($i <= 100); $i++) {
        $cost_range[$i] = $i;
    }

    $this->setWidgets(array(
        'user_id' => new sfWidgetFormDoctrineChoice(array('model' => 'Person', 'add_empty' => '-- Please select --'), array('onchange' => 'filerUnitCostByName()', 'id' => 'user_id')),
        'unit_cost' => new sfWidgetFormSelect(array('choices' => $cost_range), array('id'=>'unit_cost')),
    ));

How can I display the bind value through the number drop down through the Form I am taking data from a dropdown and putting in to a session, and then retrieve the data in the action. I want to set the selected variable in a dropdown created in the form created in with widget. as u can see in the widget area. I want to add the value as default value.

1
Please write down more precisely what is your purpose, what do you want to achieve. I'm not sure your problem is about binding the form.1ed
What the difference with the other question you askted How to bind a array with a single data to a form using the Action file in Symfony ?j0k

1 Answers

2
votes

For bind variables from your POST request, you can do this :

$form = new MyForm();
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

If you want bind one extra variable, you can do this :

$form = new MyForm();
$form->bind(array_merge($request->getParameter($form->getName()), array('foo' => $foo)), $request->getFiles($form->getName()));

Edit : Without POST variables, you can do :

$form = new MyForm();
$form->bind(array('foo' => $foo));