【发布时间】:2018-12-13 11:04:06
【问题描述】:
我有一个使用 formbuilder 创建的选择框:
<select id="form_type" name="form[type]" class="form-control select2 select2-hidden-accessible" tabindex="-1" aria-hidden="true">
<option value="1">text</option>
<option value="2">hidden</option>
<option value="3">password</option>
<option value="4" selected="selected">icon</option>
<option value="5">select</option>
</select>
这是在我的控制器中创建它的方式:
$formBuilder->add('type', EntityType::class, array(
'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
'class' => FieldTypes::class,
'choice_label' => function ($fieldTypes) {
return $fieldTypes->getName();
}
));
$formBuilder->add('cancel', ButtonType::class, array('label' => 'cancel','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar')))
->add('save', SubmitType::class, array('label' => 'Save','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')));
$form = $formBuilder->getForm();
$form->handleRequest($request);
但是当我想保存选择时,我收到错误消息:
传递给 App\Entity\Fields::setType() 的参数 1 必须是一个实例 App\Entity\FieldTypes 或 null,给定字符串,调用 /Users/work/project/src/Controller/PagesController.php 第 242 行
在我的实体中:
public function setType(?FieldTypes $type): self
{
$this->type = $type;
return $this;
}
它是这样存储的:
$entity = $this->getDoctrine()->getRepository($EntityName)->find($data['form[id]']);
$em = $this->getDoctrine()->getManager();
foreach ($fields as $field) {
$em = $this->getDoctrine()->getManager();
$func = 'set'.$field['fieldName'];
$args = $data['form['.$field['fieldName'].']'];
$entity->$func($args);
}
$em->flush();
【问题讨论】:
标签: symfony controller doctrine entity formbuilder