1

To set a value in a form programmatically with Symfony2, after a form has been created, I've found that the following way works:

$form_data = $form->getData();
$form_data['name'] = 'new value';
$form->setData($form_data);

This is awkward, isn't there a simpler way like $form->set('name', 'new value'); ?

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • Possible duplicate: http://stackoverflow.com/questions/7913086/how-to-set-default-value-for-form-field-in-symfony2?rq=1 – Peon Jan 17 '13 at 15:25
  • 1
    @DainisAbols nope, I saw that question. I don't want a default value, I want to change after the form has been set up. – stivlo Jan 17 '13 at 15:27

1 Answers1

5
$form->get('name')->setData('new value');
AlterPHP
  • 12,667
  • 5
  • 49
  • 54