Symfony2,如何编辑表单中的字段并避免空密码字段

时间:2019-05-16 21:49:57

标签: php symfony symfony-2.7

每当我想编辑诸如(name,username,email ..)之类的格式的字段时,我都会遇到问题。password字段在数据库中为空。我应该怎么做才能保持原样(我不想更改它)我希望只能编辑我选择的字段。

PS:我在用户名字段中也遇到了这个问题,但是我设法将其设置为“只读”来解决它,但是我在密码字段中却做不到。并且这些字段属于两个不同的实体User和Developper。 我将共享TWIG(form edit),formBuilder(UserType)和UserController

    {{ form_start(form) }}

    <div id="main-wrapper" >
        <div class="page-wrapper">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body formEdit">
                            <form class="m-t-40" novalidate>



                      <div class="form-group form-md-line-input has-success" >
                        <label class="col-md-2  control-label" for="form_control_1">Nom</label>
                        <div   class="col-md-8" >
                            <input  type="text" class="form-control" id="form_control_1"  required data-validation-required-message="This field is required" {{ form_widget(form.firstname,{'attr':{'class':'form-control'}}) }}
                            <div class="form-control-focus"> </div>
                        </div>
                         </div>
                        <div class="form-group form-md-line-input has-success">
                        <label class="col-md-2 control-label" for="form_control_1">Prénom</label>
                        <div class="col-md-8">
                            <input type="text" class="form-control" id="form_control_1"  required data-validation-required-message="This field is required" {{ form_widget(form.lastname,{'attr':{'class':'form-control'}}) }}
                            <div class="form-control-focus"> </div>
                        </div>
                         </div>
                           <div class="form-group form-md-line-input has-success" >
                        <label class="col-md-2  control-label" for="form_control_1">id</label>
                        <div   class="col-md-8" >
                            <input  readonly="" type="text" class="form-control" id="form_control_1"  required data-validation-required-message="This field is required" {{ form_widget(form.user.username,{'attr':{'class':'form-control'}}) }}
                            <div class="form-control-focus" > </div>
                        </div>
                       </div>
                        <div class="form-group form-md-line-input has-success" >
                        <label class="col-md-2  control-label" for="form_control_1">Solde conge annuel</label>
                        <div   class="col-md-8" >
                            <input   type="text" class="form-control" id="form_control_1"  required data-validation-required-message="This field is required" {{ form_widget(form.user.soldecongeannuel,{'attr':{'class':'form-control'}}) }}
                            <div class="form-control-focus" > </div>
                        </div>
                       </div>
      <div class="text-xs-right col-xs-12 selectbutton" align="center">

                                    <button type="submit" id="btn-save" name="btn-save" class="btn btn-primary">Sauvgarder</button>
                                    <button onclick="history.go(-1);" type="button"  class="btn btn-default">Annuler</button>


                                </div>

                            </form>
                        </div></div></div></div>
        </div></div>
  public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('email', 'email', array(
                'label'=>'Email *', 
                'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_email(value)'),
                'label_attr'=>array('class'=>'label-control red')))
            ->add('username', 'text', array(
            'label'=>"Identifiant *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add ('password', 'repeated', array (
                    'type'            => 'password',
                    'first_name'      => "password",
                    'second_name'     => "confirm",
                    'first_options' => array('label' => 'Mot de passe *',  
                        'attr'=>array('class'=>'form-control'),
                        'label_attr'=>array('class'=>'label-control red')),
                    'second_options' => array('label' => 'Confirmation mot de passe *',  
                        'attr'=>array('class'=>'form-control'),
                        'label_attr'=>array('class'=>'label-control red')),
                    'invalid_message' => "Mot de passe incorrect !", 
                    'attr'=>array('class'=>'form-control'),
                    'label_attr'=>array('class'=>'label-control')
                ))
            ->add('soldecongeannuel', 'integer', array(
            'label'=>"Solde congé annuel *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add('soldemaladie', 'integer', array(
            'label'=>"Solde maladie *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add('soldeautremotif', 'integer', array(
            'label'=>"Solde autre motif *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
        ;
    }
    public function editAction($username, Request $request) {
        //$em = $this->getDoctrine()->getManager();
        $userManager = $this->get('fos_user.user_manager');
        $user = $this->getUser();
        if(!is_object($user))
            return $this->redirect($this->generateUrl('fos_user_security_login'));

1 个答案:

答案 0 :(得分:0)

您可以从UserType类中删除不想编辑的字段,或构建另一个formType类以在editAction中使用。

例如,没有密码字段:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array(
                'label'=>'Email *', 
                'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_email(value)'),
                'label_attr'=>array('class'=>'label-control red')))
            ->add('username', 'text', array(
            'label'=>"Identifiant *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add('soldecongeannuel', 'integer', array(
            'label'=>"Solde congé annuel *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add('soldemaladie', 'integer', array(
            'label'=>"Solde maladie *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
            ->add('soldeautremotif', 'integer', array(
            'label'=>"Solde autre motif *", 
            'attr'=>array('class'=>'form-control',
                          'onchange'=>'check_username(value)'),
            'label_attr'=>array('class'=>'label-control red')))
        ;
    }

您还应该查看symfony表单文档,尤其是渲染部分,并相应地调整树枝以确保正确渲染所有字段。