硅形 - 方法错误(开机自检/获取)



我对Silex表单有问题。(易于测试)。

注释来自捆绑包:https://github.com/danadesrosiers/silex-annotation-provider

这是我的功能:

/**
     * @SLXRoute(
     *      @SLXRequest(method="GET", uri="add"),
     *      @SLXBind(routeName="departement.add")
     * )
     */
    public function add(Application $app, Request $request)
    {   
        $data = [];
        $form = $app['form.factory']->createBuilder(FormType::class, $data)
        ->add('nom_dep',null,array('label' => 'Nom :'))
        ->getForm();
        $form->handleRequest($request);
        if ($form->isValid()) {
            $data = $form->getData();
            dump("test");die();
            return $app->redirect($app["url_generator"]->generate("departement.index"));
        }
        // display the form
        return $app['twig']->render('departement/new.html.twig', array('form' => $form->createView()));
    }

这是我的观点:

{{ form_start(form, { 'attr': { 'class': 'form-horizontal form-condensed', 'role': 'form' } }) }}
    <fieldset>
    <legend>Création</legend>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label for="{{ form.nom_dep.vars.id }}" class="col-lg-2 control-label">Nom :</label>
                    <div class="col-lg-10">
                       {{ form_widget(form.nom_dep,{'attr': {'class': 'form-control'}}) }}
                    </div>
                </div>
                <div style="margin-top: 50px;" class="form-group">
                    <div class="col-lg-10 col-lg-offset-2">
                    <input type="submit" value="Créer" class="btn btn-info" />
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
{{ form_end(form) }}

而表格的结果:

<form name="form" method="post" class="form-horizontal form-condensed" role="form">
    <fieldset>
    <legend>Création</legend>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label for="form_nom_dep" class="col-lg-2 control-label">Nom :</label>
                    <div class="col-lg-10">
                       <input type="text" id="form_nom_dep" name="form[nom_dep]" required="required" class="form-control">
                    </div>
                </div>
                <div style="margin-top: 50px;" class="form-group">
                    <div class="col-lg-10 col-lg-offset-2">
                    <input type="submit" value="Créer" class="btn btn-info">
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
<input type="hidden" id="form__token" name="form[_token]" value="CGhbs1VCxoJ1DFHkLKodt9bRaEZCH1JEoqYJh8TK7I8"></form>

但是当我提交表单时,我收到以下错误:

No route found for "POST /departement/add": Method Not Allowed (Allow: GET)

这是正常的,因为我的路由是GET方法。如果我更改为 POST,我无法显示视图,因为它是 GET 方法。

欢迎任何帮助。谢谢!

如果我正确阅读了文档,您应该能够注册多个请求方法,如下所示

/**
     * @SLXRoute(
     *      @SLXRequest(method="GET", uri="add"),
     *      @SLXRequest(method="POST", uri="add"),
     *      @SLXBind(routeName="departement.add")
     * )
     */
    public function add(Application $app, Request $request)
    {  

@Request注释将 uri 模式与控制器方法相关联。如果给出了多个@Request注释,则所有修饰符将应用于所有@Requests,除非它们包装在@Route注释中。

最新更新