douyuefei3546 2017-11-10 11:47
浏览 49

Symfony - 如何从视图中的select html标记中获取数据

I am learning Symfony so I am testing various ways to work with database. The idea is to make select dropdown list that contains data from table in database. Each option in select returns value that is id of column from table. Dropdown list is defined in View in this way ('find_res' in action is route to controller):

<form action="{{ path('find_res') }}" method="POST" name="formpt">
       <select name="patientId">
        {% for patient in patients %}       
           {{dump(patient)}}
           <option value="{{patient.id}}">{{patient.lastname}}{{patient.firstname}}</option><br />
        {% endfor %}
      </select>   
  <button type="submit" name="submit">Find results</button>
  </form>

so that for chosen patient.id it should show results in table which is defined in code bellow this as standard html table.

In controller method I created list of all patients from table in database and sent it as parameter to this view. And that works just fine. Also, I would not get into details how query is built because the problem is that any $request I try does not return anything, just null. I tried:

$request->request->get('patientId'); //returns null always
$request->get('patientId'); //also null

I even tried to give name to form in view like for instance 'formpt' and then tried it with:

$request->request->get('formpt')['patientId'];

still nothing. This test code:

if($request->getMethod() == 'POST'  ) {
        \Doctrine\Common\Util\Debug::dump($request->request->get('patientId'));
        exit;
  }

always returns NULL or string(0) "" for whatever I put in "dump".

What I am doing wrong? Thanks!

  • 写回答

2条回答 默认 最新

  • doushi1964 2017-11-10 12:00
    关注

    Maybe try this

    For mapped field

    public function yourAction(Request $request){
            $patient = new Patient();
            $form = $this->createForm('AppBundle\Form\PatientType', $patient);
            $form->handleRequest($request);
    
            if ($form->isSubmitted())
            {
             $patientId = $patient->getId();
            }
    }
    

    If you need to get a non mapped field (field that's not a Patient property)

    public function yourAction(Request $request){
                $patient = new Patient();
                $form = $this->createForm('AppBundle\Form\PatientType', $patient);
                $form->handleRequest($request);
    
                if ($form->isSubmitted())
                {
                 $patientCustomField = $form['customField']->getData();
                }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?