代码之家  ›  专栏  ›  技术社区  ›  Owais Aslam

带媒体获取错误的表单:CSRF令牌无效。请尝试重新提交表单

  •  0
  • Owais Aslam  · 技术社区  · 7 年前

    错误:CSRF令牌无效。请尝试重新提交表单。

    我知道很多人以前都问过这个问题,但我没有从他们那里得到答案。我尝试添加手动标记字段,并尝试验证它,如前所述 here

    这是我的树枝:

    {{ form_start(jobApplicationForm, {'attr': {'id': 'jobApplication','class':'', 'method': 'post','enctype':'multipart/form-data' }}) }}
            <div id="job_application_first_name_validate"></div>
            {{ form_widget(jobApplicationForm.first_name, {'value' : linkedinUserFirstName}) }}
            <div id="job_application_last_name_validate"></div>
            {{ form_widget(jobApplicationForm.last_name, {'value' : linkedinUserLastName}) }}
            <div id="job_application_email_address_validate"></div>
            {{ form_widget(jobApplicationForm.email_address, {'value' : linkedinUserEmail}) }}
            <div id="job_application_resume_binaryContent_validate"></div>
            <div id="job_application_cover_letter_binaryContent_validate"></div>
            <div class="options clear">
    
                <label for="job_application_cover_letter_binaryContent"><a
                            class="icon_paperclip letter">Include a cover letter</a></label>
    
                {{ form_widget(jobApplicationForm.cover_letter) }}
                {{ form_widget(jobApplicationForm.job_id) }}
                <label for="job_application_resume_binaryContent"><a class="icon_paperclip resume">Attach
                        your resume</a></label>
                {{ form_widget(jobApplicationForm.resume) }}
            </div>
    
            <div class="attachments clear list">
            </div>
            {#                      <input type="hidden" name="token" value="{{ csrf_token('_jobApplication_token') }}"/>#}
            <div id="hiddenRecaptcha_validate"></div>
            <div class="captcha g-recaptcha" data-sitekey="{{ GOOGLE_CAPTCHA_SITEKEY }}"></div>
            <input type="hidden" class="hiddenRecaptcha required" name="hiddenRecaptcha"
                   id="hiddenRecaptcha"/>
            <div class="center">
                <button type="submit" class="btn btnGreen btnSubmit"><span>Apply</span></button>
            </div>
            {{ form_rest(jobApplicationForm) }}                          
     {{ form_end(jobApplicationForm) }}
    

    $('#jobApplication').on('submit', function (e) {
        e.preventDefault();
        if ($('#job_application_resume_binaryContent').val() != "") {
            if ($(this).valid()) {
                var $form = $(e.target),
                    form = $('#jobApplication'),
                    formId = '#' + $form[0].id;
                var holder = $(this).closest('.formHolder');
                holder.removeAttr('style').removeClass('act').css('overflow', 'hidden').addClass('act');
                var formData = new FormData(form[0]);
                holder.find('.form').delay(400).fadeOut(400, function () {
                    holder.find('.loading').fadeIn(400);
                    $.ajax({
                        type: 'post',
                        url: $form.attr('action'),
                        data: formData,
                        enctype: 'multipart/form-data',
                        processData: false,  // Important!
                        contentType: false,
                        cache: false,
                        dataType: 'JSON',
                        success: function (res) {
                            if (res.status === true) {
                                $('.thanks').show();
                            } else {
                                grecaptcha.reset();
                            }
                        }
                    });
                });
            }
        } else {
            $('.options .resume').css({
                'color': '#FF484D',
                'font-weight': 'bold'
            });
        }
    });
    

    我的控制器:

    if ($form->isValid()) {
        //                    if ($this->isCsrfTokenValid('_jobApplication_token', 
          $submittedToken)) {
            $resume = $form->get('resume')->getData();
            $cover_letter = $form->get('cover_letter')->getData();
            $attachments = null;
            $provider = $this->getContainer()->get('sonata.media.provider.file');
    
            if (!empty($resume)) {
                $attachments[0]['fileName'] = $resume->getName();
                $attachments[0]['mimeType'] = $resume->getBinaryContent()->getMimeType();
                $attachments[0]['filePath'] = $_SERVER['DOCUMENT_ROOT'] . $provider->generatePublicUrl($resume, 'reference');
    
            }
            if (!empty($cover_letter)) {
                $attachments[1]['fileName'] = $cover_letter->getName();
                $attachments[1]['mimeType'] = $cover_letter->getBinaryContent()->getMimeType();
                $attachments[1]['filePath'] = $_SERVER['DOCUMENT_ROOT'] . $provider->generatePublicUrl($resume, 'reference');
    
            }
            $data = $form->getData();
            // ... perform some action, such as saving the task to the database
            // for example, if Task is a Doctrine entity, save it!
            $entityManager = $this->getDoctrine()->getManager();
            $data->setCreatedAt(new \DateTime('now'));
            $data->setUpdatedAt(new \DateTime('now'));
            $data->setSite($this->getSiteByLocale());
            $entityManager->persist($data);
            $entityManager->flush();
            $fields = [
                [
                    'id' => '360014538174',
                    'value' => $data->getFirstName()
                ],
                [
                    'id' => '360014499253',
                    'value' => $data->getLastName()
                ],
                [
                    'id' => '360014289413',
                    'value' => $data->getEmailAddress()
                ]
            ];
            $result = $this->CreateTicketOnZendesk('Career Inquiry', 'Career form inquiry', '360000622393', $fields, $attachments, $data->getEmailAddress());
            $result = $this->CreateUserOnZendesk($data->getFirstName() . ' ' . $data->getLastName(), $data->getEmailAddress(), 'Careers');
            $emailResponse = $this->sendEmail2User($this->fromEmail, $data->getEmailAddress(), 'Careers', $data, $baseurl);
            $success = true;
    
            //          $redirect_url = $this->generateUrl("cms_help_center", array(), true);
            return new JsonResponse(array('status' => $success));
            } 
            else {
            die($form->getErrors());
            return new JsonResponse(array('status' => false, 'message' => !empty($message) ? $message : 'Something went wrong'));
    }
    

    我陷入了这个问题。

    0 回复  |  直到 7 年前
    推荐文章