Symfony2 Ajax form & Edit action



我正在使用symfony 2.3,我正在使用Ajax表单。这是我的模式,我使用FOSUserBundle,并为用户配置文件添加了一个额外的表。在showProfile操作中,我从数据库中获得相应的UserProfile,如果它已创建,并将表单发送到视图,现在一切都很好。

为了处理表单请求,我创建了另一个返回JsonResponse的操作,这个操作处理UserProfile表单请求,我的代码看起来像这样:

public function processProfileAction(Request $request)
    {
        $userManager = $this->container->get('fos_user.user_manager');
        $user = $userManager->findUserByUsername($this->container->get('security.context')->getToken()->getUser());
        $userProfile = new UserProfile();
        $formFactory = $this->container->get('fos_user.profile.form.factory');
        $postform = $formFactory->createForm();
        $postform->setData($userProfile);
        if ($request->isMethod('POST') ) 
        { 
            $postform->bind($request);
            $response = new JsonResponse();
            if ($postform->isValid()) 
            {                
                $userProfile->setUser($user);
                $em = $this->container->get('doctrine')->getManager();
                $em->persist($userProfile);
                $em->flush();
                $response->setData(array("Success" => true, "Message" => "Your data was saved successfully"));
            }
            else {
                $response->setData(array("Success" => false, "Cause" => "Invalid form data"));
            }
            return $response;
        }
        $response = new JsonResponse();
        $response->setData(array("Success" => false, "Cause" => "Method is not POST"));
        return responde;
    }

如果我想编辑用户配置文件,错误提示:

Integrity constraint violation: 1062 Duplicate entry

这是视图中的表单:

<form id="user_profile_form" action="{{ path('_process_user_profile') }}" {{ form_enctype(userProfileForm) }} method="POST">
                    {{ form_errors(userProfileForm) }}
                    {{ form_widget(userProfileForm._token) }}
                    <div class="row">
                        <div class="col-xs-5">
                            <div class="widget_error">{{ form_errors(userProfileForm.first_name) }}</div>
                            <div class="form-row" style="width:250px;">
                                {{ form_widget(userProfileForm.first_name, {'label': null, 'attr': {'class': 'form-control', 'placeholder': 'placeholder.first_name'|trans}} ) }}
                            </div>
                        </div>
                        <div class="col-xs-5">
                            <div class="widget_error">{{ form_errors(userProfileForm.last_name) }}</div>
                            <div class="form-row" style="width:250px;">
                                {{ form_widget(userProfileForm.last_name, {'label': null, 'attr': {'class': 'form-control', 'placeholder': 'placeholder.last_name'|trans}} ) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-3"><button type="submit" class="btn btn-info">{{ 'buttons.save_changes'|trans }}</button></div>
                    </div>
                    </form>

我知道用户配置文件已经存在,我正在尝试创建一个具有相同user_id的新配置文件,我该如何解决这个问题?我错过什么了吗?

谢谢!

我修复了自己的代码。在绑定表单之前,我没有创建新的UserProfile实例。

我是这样做的:

$em = $this->container->get('doctrine')->getManager();            
$userProfile = $em->getRepository('AloBundle:UserProfile')->findOneBy(array("user" => $user->getId()));
if ((!isset($userProfile))) $userProfile = new UserProfile();

我尝试从数据库中检索实际的UserProfile,如果它不存在,我创建一个新的。

相关内容

最新更新