【问题标题】:Not able to save data(drop down values) to DB in symfony2 while uing ajax使用 ajax 时无法将数据(下拉值)保存到 symfony 2 中的数据库
【发布时间】:2015-12-28 07:27:26
【问题描述】:

我制作了一个注册页面,用户必须在其中输入国家/地区详细信息。用户将使用下拉菜单输入国家/地区,这将使用 ajax 获取州下拉列表,城市列表也是如此。 我正在获取列表,但是当我尝试保存表单时,它显示城市和州字段上的值无效。 请有人帮助我。谢谢。 这是我的控制器

public function getStateAction(Request $request)
    {               
            $em = $this->getDoctrine()->getManager();
            $data = $request->request->all();   

            $countryId = $data['id'];
            $repository = $em->getRepository('FOSUserBundle:State');
            $query = $repository->createQueryBuilder('p');
            $query->where('p.countryId ='.$countryId);
            $query->orderBy('p.stateName', 'ASC');
            $stateList = $query->getQuery('p')->getResult();
            $html='';

            foreach($stateList as $list){
                $html .="<option value=".$list->getId()." >".$list->getStateName()."</option>";
            }
            return new JsonResponse( $html );       
    }

    public function getCityAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

            $data = $request->request->all();   

            $stateId = $data['id'];
            $repository = $em->getRepository('FOSUserBundle:City');
            $query = $repository->createQueryBuilder('p');
            $query->where('p.stateId ='.$stateId);
            $query->orderBy('p.cityName', 'ASC');
            $stateList = $query->getQuery('p')->getResult();
            $html='';
            foreach($stateList as $list){
                $html .="<option value=".$list->getId()." >".$list->getCityName()."</option>";
            }
            return new JsonResponse( $html );

    }

这是我的 Ajax。

{% block javascripts %}   
    <script type = 'text/javascript'>
    $(document).ready(function(){      $("#fos_user_registration_form_country_id").change(function(){
                    $("#fos_user_registration_form_state_id").empty();
                    var countryId = $(this).val();  

                    if(countryId!=0){
                      $.ajax({
                         type: "POST",
                         url: "{{ path('fos_user_registration_country') }}",
                         data: {id: countryId},
                         cache: false,
                         success: function(result){

                           $("#fos_user_registration_form_state_id").append(result);

                         }
                       });
                    }
                    });
                    $("#fos_user_registration_form_state_id").change(function(){
                     var stateId = $(this).val();
                     $("#fos_user_registration_form_city_id").empty();
                     if(stateId!=0){
                     $.ajax ({
                       type:"POST",
                       url: "{{ path('fos_user_registration_state') }}",
                       data: {id: stateId}, 
                       cache: false,
                       success: function(result){
                       $("#fos_user_registration_form_city_id").append(result);
                         }
                       });
                    }
                    else{
                        alert('Please select country');
                        return false;
                    }
                });
        });
        </script>
    {% endblock %}

我已经制作了一个注册表单,每当我尝试保存一个人的详细信息时,我都会收到一个错误,指出 city_idstate_id 具有无效值

【问题讨论】:

  • 澄清一下,city_id的数据类型是IntegerType吗?您可能需要将从 ajax 请求中收到的 String 值类型转换为 Integer。
  • 是的@WillyPt city_id 和 stae_id 的值是整数。
  • 加载附加数据的表单类型定义如何?
  • 这是我的表单类型的定义@xabbuh $builder->add('state_id', 'choice', array('label'=>false,'required'=>false, 'choices ' => array("" => '选择状态', ))); $builder->add('city_id', 'choice', array('label'=>false,'required'=>false, 'choices' => array("" => 'Select City', )));
  • 问题在于 Symfony Form 组件不知道哪些选项是有效的(只有前端的选项被填充,PHP 后端只知道空的占位符选项,顺便说一下,您应该为此使用placeholder 选项)。

标签: php jquery ajax symfony


【解决方案1】:

问题是 stateId 和 cityId 不会进入 db。 stateId 和 cityId 是与不同表关联的外键。我能够通过以下代码克服这个问题。

public function saveAction(Request $request)
        {           

                $data = $request->request->all();   


                $userManager = $this->container->get('fos_user.user_manager');
                $em = $this->getDoctrine()->getManager();

                $user = $userManager->createUser();

                $countryId =$data['fos_user_registration_form']['country_id'];
                $stateId =$data['fos_user_registration_form']['state_id'];
                $cityId =$data['fos_user_registration_form']['city_id'];

                $country= $this->getDoctrine()->getRepository('FOSUserBundle:Country')->find($countryId);
                $user->setCountry($country);

                $state= $this->getDoctrine()->getRepository('FOSUserBundle:State')->find($stateId);
                $user->setState($state);

                $city= $this->getDoctrine()->getRepository('FOSUserBundle:City')->find($cityId);
                $user->setCity($city);

     /.../
}

【讨论】:

  • 感谢您的回答,对我有帮助。
【解决方案2】:

所以我要解决的问题是用可用状态的完整列表填充状态类型(如果我正确理解您当前的解决方案,这已经发生在 AJAX 中,所以对用户来说没有真正的区别) .城市列表仍应通过 AJAX 请求获取,具体取决于所选状态。

但是,当提交表单时,您需要监听表单的 PRE_SUBMIT 事件,检查 state 类型的选定值并根据该值填充城市选项。这样,表单组件将能够正确验证所选城市值。

您可以在食谱中找到类似的示例:http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

【讨论】:

  • 我能够使用序列化函数保存数据我使用序列化函数来保存表单数据@xabbuh
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2019-03-23
相关资源
最近更新 更多