gpt4 book ai didi

javascript - 使用 Symfony 向输入发送值

转载 作者:行者123 更新时间:2023-12-03 03:41:14 24 4
gpt4 key购买 nike

我尝试在 Symfony 中使用javascript,然后遇到了问题。问题出在我的文件 twig.html 上。一开始我有这样的代码:

  {{ form_widget(form.name, {'attr': {'class': 'form-control'} }) }}

然后我将其更改为这样,以便使用动态验证(与浏览器中生成的代码相同,我只是添加 onkeyup 操作)

  <input type="text" id="person_name"
onkeyup="myFunction()" name="person[name]" required="required"
class="form-control" />

然后我很高兴,因为我的验证规则有效,但是当我想更新到表单时我发现了这一点。字段名称为空(但在服务器中它很好)。所以我想得到这个领域。在我的函数 updateAction 中,我在处理 form 之前转储变量 person ,然后 name 包含了好元素。所以问题是 form_widget 和我刚刚做的好的有区别。我想做这样的事情来获取我的字段名称:

   <input type="text" id="person_name"
onkeyup="myFunction()" name="person[name]" required="required"
class="form-control" value="if(form.name is not empty) form.name"/>

谢谢。

最佳答案

您是否将实体或文档传递给 createForm 函数?类似的东西应该有效:

Controller

$em = $this->getDoctrine()->getManager();
$your_entity = $em->getRepository(**your_entity_path**)->findOneBy([**parameters**]);
$edit = 1;
if(!isset($your_entity)) {
$your_entity = new Your_entity();
$edit = 0;
}
$form = $this->createForm(**your_entity_type_path**, $your_entity);
$editForm->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
if(!$edit)
$em->persist($your_entity);
$em->flush();

//other code
}

我希望这不会太困惑

关于javascript - 使用 Symfony 向输入发送值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com