duanliao3826 2019-04-01 10:12
浏览 66
已采纳

Symfony - 我不能用树枝定制我的表格

I would like to create a login form in twig (login + password) and which is customized. The form should search the Users table and the LDAP directory if the information entered exists.

Moreover, when I validate the form, I do not want the login and the password to be found in the URL.

At validation, if the user exists, he is redirected to a page, otherwise he will remain on the login page.

I managed to make a form, customized but that returns the login and password in the URL.

It looks like this:

enter image description here

And this is the code in twig :

{% extends 'base.html.twig' %}
{% block stylesheets %}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>rel="stylesheet"> 

{% endblock %}
{% block body %}

<form class="navbar-form navbar-center">
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
        <input id="text" type="text" class="form-control" name="login" value="" placeholder="Login">                                        
    </div>
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
        <input id="password" type="password" class="form-control" name="password" value="" placeholder="Mot de passe">                                        
    </div>
    <button type="submit" class="btn btn-primary">Valider</button>
<br/><br/>
    Pas de compte académique ? <a href="{{path('paquets_index')}}">Demande de création de compte</a>
</form>

{% endblock %}

Now my goal is to make sure that there is no login and password that are found in the URL to validation. It will just be a check and in function, it will show yes or no the next page. And I can not do it.

Moreover, from a functional point of view, is it useful to go through a formType builder from my Users entity? Or can I just be content with this kind of form, which would send back to the controller the information entered, to do the verification?

When in doubt, here is my form builder

<?php

namespace Site\PagesBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class ConnexionType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('identifiant', TextType::class)
        ->add('password', PasswordType::class,array(
            'attr' => array(
                'placeholder'=> 'Mot de passe',
                'label' =>'Mot de passe',
            )
        ))
        ;
    }/**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Site\PagesBundle\Entity\User'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'site_pagesbundle_user';
    }


}

Thanks for your help !

  • 写回答

2条回答 默认 最新

  • doudu9652 2019-04-01 11:11
    关注

    I'm not quite sure, how bend on reinventing the wheel you are. But symfony has a lot of stuff prepared, since this is not that uncommon of a use case.

    I like the 3.4 tutorial on building a login: https://symfony.com/doc/3.4/security/form_login_setup.html

    (at the side, there is the possibility to switch to 4.3 or whatever, however, the 3.4 tutorial should still work, even in 4.x)

    you don't particularly need your own FormType to do the login, since the Security components already handle form submissions. Also have a look at csrf protection ;o)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来