【问题标题】:Symfony 4 - KnpPaginatorBundle - This form should not contain extra fieldsSymfony 4 - KnpPaginatorBundle - 此表单不应包含额外字段
【发布时间】:2019-09-15 12:09:27
【问题描述】:

我正在为页面使用 KnpPaginatorBundle。 在这个页面中,我使用了一个手工创建的过滤器,它给出了这个

因此,如果我在表单中输入内容,它将处理请求并按照约定显示。 问题是当我要求按“ID”排序时。

当我对名称或服务进行过滤时会出现此问题,并且我要求按 ID 排序

我收到以下错误消息:

此表单不应包含额外字段

我应该怎么做才能停止这个错误?

我的控制器是:

 /**
     * Undocumented function
     * 
     * @Route("/rh3", name="rh_index3")
     *
     * @param PaginatorInterface $paginator
     * @param Request $request
     * @param ObjectManager $manager
     * @param UserRepository $repo
     * @return void
     */
    public function index3(PaginatorInterface $paginator, Request $request, UserRepository $repo)
    {

        $search = new UserSearch();
        $form = $this->createForm(UserSearchType::class, $search);
        $form->handleRequest($request);




        $users = $repo->getUsersSearch($search);

        $pagination = $paginator->paginate(
            $users,
            $request->query->getInt('page', 1),
            7
        );

        return $this->render('rh/index3.html.twig', [
            'pagination' => $pagination,
            'form' => $form->createView(),
        ]);
    }

还有我的树枝:

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

{% block body %}

    <div class="jumbotron">

        <div class="container">
            {{form_start(form)}}
            <div class="form-row align-items-end">
                <div class="col">
                    {{form_row(form.name)}}
                </div>
                <div class="col">
                    {{form_row(form.service)}}
                </div>
                <div class="col">
                    <div class="form-group">

                        <button type="submit" class="btn btn-primary">Rechercher</button>
                    </div>
                </div>

            </div>
            {{form_end(form)}}
        </div>

    </div>

    <table class="table table-hover align-items-center">
        <thead>
            <tr>
                <th>{{ knp_pagination_sortable(pagination, 'Id', 'u.id') }}</th>
                <th>Nom</th>
                <th>Service</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            {% for user in pagination %}
                <tr {% if loop.index is odd %} class="color" {% endif %}>
                    <td>{{user.id}}</td>
                    <td>{{user.fullName}}</td>
                    <td>{{user.groupe.nom}}</td>
                    <td>
                        <a href="#" class="btn btn-primary">Voir</a>
                    </td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

    <div class="navigation">
        {{knp_pagination_render(pagination)}}
    </div>

{% endblock %}

感谢您的帮助!

【问题讨论】:

    标签: php symfony pagination


    【解决方案1】:

    在你的 UserSearchType 添加这个选项:

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'allow_extra_fields' => true,
        ));
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多