0
我想保存一些數據,其中一部分來自用戶,他們通過表單提交,另一部分在實際控制器中生成。因此,像:將數據添加到Symfony2中的控制器中的提交表單對象
# controller
use Acme\SomeBundle\Entity\Variant;
use Acme\SomeBundle\Form\Type\VariantType;
public function saveAction()
{
$request = $this->getRequest();
// adding the data from user submitted from
$form = $this->createForm(new VariantType());
$form->bindRequest($request);
// how do I add this data to the form object for validation etc
$foo = "Some value from the controller";
$bar = array(1,2,3,4);
/* $form-> ...something... -> setFoo($foo); ?? */
if ($form->isValid()) {
$data = $form->getData();
// my service layer that does the writing to the DB
$myService = $this->get('acme_some.service.variant');
$result = $myService->persist($data);
}
}
如何獲得$foo
和$bar
到$form
對象,這樣我可以驗證它,並堅持呢?