我有一个email
对象,其中包含一个集合textareas
,并且具有一个属性template
。集合textareas
包含template
中的textareas,它们在控制器中设置。
public function editAction($id, Request $request)
{
// Get the email
$email = EmailQuery::create()->findPk($id);
if (!$email)
{
throw $this->createNotFoundException('Unknown email ID.');
}
// If a new template file is selected, the client refreshes the form with ajax
// So let's get the new template value
if (isset($request->request->get('email')['template']))
{
if (isset($request->request->get('email')['update_with_ajax']))
$email->setTemplate($request->request->get('email')['template']);
}
// Get textareas in the email's template
$email->setTextareas($this->get('email.analyzer')->getTextAreas($email->getTemplate()));
// Create form
$form = $this->createForm('email', $email);
// Handle form
$form->handleRequest($request);
if ($form->isValid() && $form->get('save')->isClicked())
{
$form->save();
}
}
这是我的电子邮件类型(这是一项服务):
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class EmailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Read template files in dir and store names in $templates
// $templates = array();
$builder->add('template', 'choice', array('choices' => $templates);
$builder->add('textareas', 'collection');
$builder->add('save', 'submit');
$builder->add('update_with_ajax', 'submit');
}
}
这里的问题是我想在用户选择另一个模板时通过AJAX更新textareas
集合。正如您所看到的,我在创建表单之前将模板集合添加到电子邮件对象中,我认为这一切都会出错并触发This form should not contain extra fields
错误。但是什么是正确的方法?