dpus81500574 2018-05-20 08:30
浏览 97
已采纳

PHP symfony 4 ajax表单提交

I'm trying to submit a form using ajax and send it's data to the database, but I don't understand how to proces the data received from the ajax call.

I wrote the following code:

{% extends 'base.html.twig' %}

{% block title %}Assignments | CRM Fabriek{% endblock %}

{% block body %}

    <div class="container">
        <div class="columns">
            <div class="column is-full">
                <div class="level">
                    <h1 class="level-left title title-no-margin is-vcentered">Assignments</h1>

                    <div class="level-right">
                        {% include 'search.html.twig' %}

                        <a href="{{ path("newAssignment") }}" class="level-item button is-success">Add new</a>
                    </div>
                </div>
                <table class="table is-fullwidth">
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                    {% if assignments != null %}
                        {% for assignment in assignments %}
                            <tr>
                                <td>{{ assignment.name }}</td>
                                <td>{{ assignment.description }}</td>
                                <td>{{ assignment.status }}</td>
                                <td>
                                    <a class="button is-info is-small" href="{{ path('overviewAssignment', {'id': assignment.id}) }}"><i class="fa fa-eye"></i></a>
                                    <a class="button is-warning is-small" href="{{ path('editAssignment', {'id': assignment.id}) }}"><i class="fa fa-pencil"></i></a>
                                    <button class="button is-success is-small" onclick="openModal({{ assignment.id }})"><i class="fa fa-plus"></i></button>
                                </td>
                            </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="4">No entries</td>
                        </tr>
                    {% endif %}
                </table>
                <div class="pagerfanta">
                    {{ pagerfanta(pager)}}
                </div>
                <div>
                    <div class="modal">
                        <div class="modal-background"></div>
                        <div class="modal-card">
                            <header class="modal-card-head">
                                <p class="modal-card-title">Modal title</p>
                            </header>
                            <section class="modal-card-body">
                                {{ form_start(form, {'attr': {'id': 'task_form'}}) }}
                                {{ form_row(form.name, {'attr': {'class': 'input'}}) }}
                                {{ form_row(form.description, {'attr': {'class': 'textarea'}}) }}

                                {{ form_label(form.status) }}
                                <div class="control">
                                    <div class="select">
                                        {{ form_widget(form.status) }}
                                    </div>
                                </div>

                                {{ form_end(form) }}
                            </section>
                            <footer class="modal-card-foot">
                                <button id="submit_task" class="button is-success">Save changes</button>
                                <button class="button" onclick="closeModal()">Cancel</button>
                            </footer>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

{% block javascripts %}
    <script>
        function openModal(id){
            $('.modal').addClass('is-active');
            $('#submit_task').attr('onClick', 'submitTask('+ id +');');
        }

        function closeModal(){
            $('.modal').removeClass('is-active');

        }
        function submitTask(id){
            console.log(id);

            form = $('#task_form').serialize();

            console.log(form); 
            $.ajax({
                url:'{{ path('submit_task') }}',
                type: "POST",
                dataType: "json",
                data: {
                    "task": form
                },
                async: true,
                success: function (return_data)
                {
                    console.log(return_data);
                },
                error: function (xhr, ajaxOptions, thrownError)
                {

                }
            });
            closeModal();
        }
    </script>
{% endblock %}

With the following method in the controller:

/**
     * @Route("/", name="submit_task")
     */
    public function add_task(Request $request){
        $form_data = $request->get('task');

        return new JsonResponse($form_data);
    }

The form is created in the index action:

/**
     * @Security("is_authenticated()")
     * @Route("/assignments", name="assignments")
     */
    public function index(Request $request)
    {

        $assignment_rep = $this->getDoctrine()->getRepository(Assignment::class);

        if($request->get('search') == null) {
            if($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
                $assignments = $assignment_rep->findAll();
            }
            else
            {
                /* @var User $user */
                $user = $this->getUser();
                $assignments = array();
                foreach($user->getAssignments() as $assignment){
                    array_push($assignments, $assignment);
                }
            }
        }
        else{

            if($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
                $assignments = $assignment_rep->search($request->get('search'));
            }
            else
            {
                /* @var User $user */
                $assignments = $assignment_rep->searchUser($request->get('search'), $this->getUser()->getId());
            }
        }

        $page = $request->query->get('page', 1);

        $adapter = new ArrayAdapter($assignments);

        $pagerfanta = new Pagerfanta($adapter);
        $pagerfanta->setMaxPerPage(25);
        $pagerfanta->setCurrentPage($page);


        $task = new Task();
        $form = $this->createForm(TaskFormType::class, $task);
        $form->remove('assignment');

        $assignments = $pagerfanta->getCurrentPageResults();

        return $this->render('assignment/index.html.twig', array(
            'assignments' => $assignments,
            'pager' => $pagerfanta,
            'form' => $form->createView()
        ));
    }

I would like to know how to process the form data without the "form" object in the function. Could anyone help me out with this problem!

  • 写回答

1条回答 默认 最新

  • douzhi9921 2018-05-20 08:47
    关注

    Your problem is something I stumbled upon back in Symfony3 and since then I've been trying various methods in handling content sent via AJAX.

    Get AJAX data

    This part is simple, in the controller simply call $data = $request->getContent();

    Create a service

    Create a service to help handle this kind of data. It uses Symfony's validator (see constructor: ValidatorInterface), and has a method called validateAndCreate that takes in as a parameter $data (which is the AJAX request body content) and $entityClassName (which is the entity to create and populate with data, example: User::class will generate the class name string)

    Normalizer, Encoder and Serializer

    These three will be important in handling your AJAX content. $data can be deserialized and injected directly into an entity instance (created from the ClassName parameter)

    If data is sent as JSON then use the JsonEncoder, once the serializer object is created then it'll be able to deserialize the JSON data directly into the newly instantiated object (the className in parameter is used to generate the object).

    Once the object is generated, use the validator to check and see if it's a valid object. I recommend to use the @Assert in all Entity objects for the validator to work accordingly.

    class ApiService
    {
    
        private $validator;
        public function __construct(ValidatorInterface $validator)
        {
            $this->validator = $validator;
        }
    
        public function validateAndCreate($data, $entityClassName){
    
            $objectNormalizer = new ObjectNormalizer();
            $normalizers = [$objectNormalizer];
            $encoders = [new JsonEncoder()];
            $serializer = new Serializer($normalizers, $encoders);
    
            $result = $serializer->deserialize($data, $entityClassName, 'json');
            $errors = $this->validator->validate($result);
    
            if(count($errors) > 0){
                throw new CustomApiException(Response::HTTP_BAD_REQUEST, (string) $errors);
            }
    
            return $result;
    
        }
    }
    

    Example in use in a controller for creating a new meeting

    public function newMeeting(Request $request, ApiService $apiService, FractalService $fractalService){
            /** @var Admin $admin */
            $admin = $this->getUser();
    
            /** @var UserRepository $rep */
            $em = $this->getDoctrine()->getManager();
    
            $data = $request->getContent();
    
            if(empty($data)) throw new CustomApiException(Response::HTTP_BAD_REQUEST, "Data sent null.");
    
            /** @var Meeting $test */
            $meeting = $apiService->validateAndCreate($data, Meeting::class);
    
            $meeting->setAdmin($admin);
            $admin->addMeeting($meeting);
    
            $em->persist($test);
            $em->flush();
    
            //Return data however you wish, I use a FractalService
    
            $data = $fractalService->generateData($meeting, MeetingTransformer::class, 'meeting');
            return $fractalService->generateResponse($data);
    
        }
    

    Client side example of form handling for AJAX

    $("#form").on("submit", function(e){
       e.preventDefault();
       let data = {};
       $(this).serializeArray().forEach((object)=>{
          data[object.name] = object.value;
       });
       console.log(data);
       
       //TODO: ajax call here with data
       //If ajax call fails because server can't decode
       //Think of doing : data = JSON.stringify(data);
       console.log(JSON.stringify(data));
       
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form id="form">
      <input type="text" value="john" name="firstname"/>
      <input type="text" value="smith" name="lastname"/>
      <input type="text" value="florida" name="address"/>
      <input type="text" value="1234512345" name="phonenumber"/>
      <input type="text" value="john.smith@gmail.com" name="email"/>
    
      <input type="submit" value="submit this"/>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏