[英]Populating form data from entity in Symfony2
I have an action that basically renders a form and I want it to be a new form if the ID is null and an edit form if the ID matches with the PK in the DB. 我有一个基本上可以呈现表单的动作,如果ID为空,我希望它是一个新表单,如果ID与数据库中的PK匹配,我希望它是一个编辑表单。 Obviously my logic is wrong because a new form is rendering every single time. 显然我的逻辑是错误的,因为每次都会渲染一个新表单。 . 。
public function editGlobalFirewallFilter(Request $request, Entities\GlobalFirewallFilter $firewall_rule = null) {
n
// Check if we have a valid rule. If not create a new blank one and associate our account id
// if( ! $firewall_rule ) {
// $results = $this->getDoctrine()->getRepository('bundle:GlobalFirewallFilter');
// $rules = $results->findAll();
// $firewall_rule = new Entities\GlobalFirewallFilter();
// }
$firewall_rule = new Entities\GlobalFirewallFilter();
// Generate our form
$form = $this->createForm(new SAForms\GlobalFirewallRuleType(), $firewall_rule);
$form->handleRequest($request);
if($form->isValid()) {
// Save our firewall rule
$em = $this->getDoctrine()->getManager();
$em->persist($firewall_rule);
$em->flush();
return $this->redirect($this->generateUrl('_dashboard__global_firewall'));
}
return array(
'title' => $firewall_rule->getFirewallFilterId() ? 'Edit Rule' : 'New Rule',
'form' => $form->createView(),
);
}
You should use the form generator command
to be oriented in the right way : 您应该使用form generator command
以正确的方式定向:
Generating a CRUD Controller Based on a Doctrine Entity 基于原则实体生成CRUD控制器
http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html use this command : http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_crud.html使用以下命令:
php app/console generate:doctrine:crud
I will generate the skeleton of you controller with all the standard actions as wanted, in your specifica case, updateAction
, newAction
, and editAction
. 我将根据需要使用所有标准动作(在您的情况下) updateAction
, newAction
和editAction
生成控制器的骨架。
I am not quite sure why are there results
and rules
- you don't use them. 我不太确定为什么会有results
和rules
-您不使用它们。 I think this code should do the trick. 我认为这段代码应该可以解决问题。
public function editGlobalFirewallFilter(Request $request, Entities\GlobalFirewallFilter $firewall_rule = null) {
// Check if we have a valid rule. If not create a new blank one and associate our account id
$firewall_rule = $firewall_rule ?: new Entities\GlobalFirewallFilter();
// Generate our form
$form = $this->createForm(new SAForms\GlobalFirewallRuleType(), $firewall_rule);
$form->handleRequest($request);
if($form->isValid()) {
// Save our firewall rule
$em = $this->getDoctrine()->getManager();
$em->persist($firewall_rule);
$em->flush();
return $this->redirect($this->generateUrl('_dashboard__global_firewall'));
}
return array(
'title' => $firewall_rule->getFirewallFilterId() ? 'Edit Rule' : 'New Rule',
'form' => $form->createView(),
);
}
PS Sadly I can't comment yet.. Can you provide controller actions where you use this function? PS遗憾的是,我无法发表评论。.您可以在使用此功能的地方提供控制器操作吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.