Symfony2, How to edit fields in a form and avoid empty password field

575 views Asked by At

I have a problem whenever I want to edit fields of a form such as (name,username,email..) the password field goes empty in the database. what should I do I want to keep it the same (I don't want to change it) I want to be able to edit only the fields I choose.

PS:I was facing this problem also with username field but I managed to solve it by making it 'read only' but I couldn't do the same with the password field. And the fields belongs to 2 different entities User and Developper. I will share the TWIG(form edit) , formBuilder(UserType), and 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

There are 1 answers

0
Bertrand On

You can remove the fields you don't want to edit from your UserType class or build another formType class to use in your editAction.

For example, without the password field :

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')))
        ;
    }

You should also take a look to the symfony form documentation, especially the rendering part, and adapt your twig accordingly to ensure all your fields are correctly rendered.