【问题标题】:Get selected option and all request data in twig form以树枝形式获取所选选项和所有请求数据
【发布时间】:2017-08-27 10:51:21
【问题描述】:

我在 twig 中有这个表单,我想从一个简单的 html 表单中获取所选选项和输入字段的值:

(PS:不要说“你最好使用控制器生成一个表单!”我知道,但我有一个理由要在 twig 中创建一个表单:因为这样我可以创建尽可能多的表单使用for循环形成我想要的形式。)

我尝试在操作路径中传递参数,但没有成功。

<form action="{{ path('changeProf') }}" method="post" id="form">
    <section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 35%;top: 6vmin">
        <label style="display:inline-table;">
            <span> <input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/></span>
        </label>
    </section>
    <section class="col-lg-9 col-md-9 col-sm-9 col-xs-9"style="position: relative; left: 35%;top: 6vmin">
        <label style="display:inline-table;">
            <span>{% set k=1 %}
                <select id="profil">
                {% for prof in profArr %}
                    <option value="{{ prof }}"> {{ prof }} </option>
                {% endfor %}
                </select>
            </span>
        </label>
    </section>
    <input type="submit" class="btn btn-info btn-xs" style="position: relative;top:18vmin;left:-28%">
</form>

这是处理表单的操作:

/**
 * @Route("/profile/chProf",name="changeProf")
 * Method ("POST")
 * @Template()
 */
public function changeProfAction(Request $request)
{
    $session = new Session();
    $session->start();
    $search = $session->get('user');
    $gestAcces = $session->get('acces');
    $gestEtat = $session->get('etatUser');
    $gestCont = $session->get('contenu');
    $repMsg = $session->get('repMsg');
    $gestRec = $session->get('Reclam');
    $gestMess = $session->get('gestMess');
    $gestMp = $session->get('gestMp');


    if ($search == Null) {
        return $this->redirectToRoute('empty', array('search' => $search,
            'contenu' => $gestCont,
            'gestAcces' => $gestAcces,
            'gestEtat' => $gestEtat,
            'repMsg' => $repMsg,
            'gestRec' => $gestRec,
            'gestMess' => $gestMess,
            'gestMp' => $gestMp,
        ));
    }

    $em = $this
        ->getDoctrine()
        ->getManager();
    $reposit = $em->getRepository("CNAMCMSBundle:userprof");
    $rep = $em->getRepository("CNAMCMSBundle:profil");

    $userprof=new userprof();
    $libprof=$request->request->get('profil');
    $iduser=$request->request->get('iduser');

    $idprof=$rep->findOneBy(array('libelle'=>$libprof));
    $userprof->setIdUser($iduser);
    $userprof->setIdProfil($idprof);
    $em->persist($userprof);
    $em->flush();

    return $this->render('CNAMCMSBundle:Default:userProf.html.twig', array(
        'search'=>$search,
        'contenu'=>$gestCont,
        'gestAcces'=>$gestAcces,
        'gestEtat'=>$gestEtat,
        'repMsg'=>$repMsg,
        'gestRec'=>$gestRec,
        'gestMess'=>$gestMess,
        'gestMp'=>$gestMp,

    ));

}

【问题讨论】:

  • 太粗鲁了!!!他们没有帮助人们解决问题,而是来投票(-1)然后走!!!!!!!
  • “获取信息”是什么意思?发布的所有内容都在超级全球$_POST
  • 第一。没有人有义务帮助您。其次:downvote 的一个可能原因是格式严重的代码(缩进)和问题主体(我试图将其分解为更易读的部分)。我没有投反对票,但我认为就目前而言你的问题很好。
  • 请贴出对应的控制器代码。
  • @Al.G.我知道没有人有义务帮助我,但与其只是投反对票,他们最好提供帮助并让我知道我的代码有什么问题。

标签: symfony twig forms


【解决方案1】:

我想我找到了导致您收到错误的原因。

  1. $request-&gt;request-&gt;get('profil'); 返回 NULL。
  2. 这意味着,表单没有发送profil条目。
  3. 查看表单中的profil 在哪里:

    <input type="text" value="{{ user.id }}" disabled="true" id="iduser"style="max-width: 18vmin;"/>
    
  4. 没有name 属性!这实际上是随请求发送的内容。 name 属性,而不是 idid 仅用于本地样式和 javascript。

  5. 解决办法:

    <input type="text" value="{{ user.id }}" disabled="true" id="iduser" name="iduser" style="max-width: 18vmin;"/>
    
  6. profil做同样的事情

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,但没有帮助,我之前尝试过,现在重试了,但我得到了同样的错误:(
  • @Susan 那么我怀疑这可能会对你有所帮助:stackoverflow.com/a/12142437/3132718 即创建一个临时表单并将其绑定到请求。然后你可以像使用普通表单类一样使用它。
  • @AI.G.非常感谢你,我想这会对我有所帮助。我会尝试发布结果
  • 不,它不起作用!你认为这是因为我在 for 循环中动态生成了表单吗???
  • 我通过添加 public function __toString() { return $this->libelle; 来修复它} 到我的实体“个人资料”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-15
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多