symfony从选择框中获取值

时间:2013-12-18 16:41:26

标签: symfony

我想从此表单中获取选择框的值

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('manifestations', 'entity', array(
                'class' => 'PrifProtocoleBundle:Manifestation',
                'multiple' => true,
                'required'=>false
            ));
}  

我试过了:

$manifestations = $form['manifestations']->getData();

$manifestations = $this->getValue('manifestations');

但没有结果!有没有人有新手的详细解决方案?感谢

3 个答案:

答案 0 :(得分:1)

$manifestations = $form['manifestations']->getData();

对我来说很好。如果在Manifestation类中你有字段$ name,那么你可以使用getName()方法。像这样:

$manifestations = $form['manifestations']->getData();
$name = $manifestations->getName();

或者你可以使用Request:

$form = $request->request->get('form');
$manifestations = $form['manifestations'];

P.S。:'form'是表单的名称。

答案 1 :(得分:0)

你可以像这样获取

use Symfony\Component\HttpFoundation\Request;

public function testAction(Request $request){
     $data=$request->request->all(); $data['form_name']['manifestations'];
    or  $data['manifestations'];
return new response(json_encode($data));
}

答案 2 :(得分:0)

Ashot的答案运作良好。 就我而言:

$currency = $form['currency']->getData();
var_dump($currency->getName());

输出:

string(3) "USD"