Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

FosUserBunde Update User Profile WITHOUT the need of current password

As I was studying the default Form Builder of FosUserBundle for Editing a User Profile (FOS\UserBundle\Form\Type\ProfileFormType) I noticed the following line of code:

$builder->add('current_password', LegacyFormHelper::getType('Symfony\Component\Form\Extension\Core\Type\PasswordType'), array(
            'label' => 'form.current_password',
            'translation_domain' => 'FOSUserBundle',
            'mapped' => false,
            'constraints' => new UserPassword($constraintsOptions),
        ));

And that render the filed "current password" But I want to edit a profile without needing to put the user password all over the time. Is there a way to do that?

Actually I tried to extend the form of FosUserBundle and I sucessfylly managed to add some new fields:

namespace AppUserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class UserProfileFormType extends AbstractType
{       
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //$builder->addViewTransformer($this->purifierTransformer);
        $builder->add('name',TextType::class,array('label'=>'profile.first_name','required' => false));
        $builder->add('surname',TextType::class,array('label'=>'profile.surnname','required' => false));
        $builder->add('email',TextType::class,['required' => false]);
        $builder->add('description',TextareaType::class,['required' => false]);

    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\ProfileFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

But I cannot find a way to get rid the password validation and the field when I need to update the user profile of the currently logged in user WITHOUT the need for the user to type all over the time the password.

Edit 1

I tried

$builder->remove('current_password');

I got the following error:

Neither the property "current_password" nor one of the methods "current_password()", "getcurrent_password()"/"iscurrent_password()"/"hascurrent_password()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".

Upvotes: 1

Views: 970

Answers (2)

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

Just as @fireaxe says. In other words put this in to your buildForm into this:

        parent::buildForm($builder, $options);        
        //Add some other fields
        $builder->remove('current_password');

Also make sure you REMOVED any {{ form_row(form.current_password) }} or any {{ form_widget(form.current_password) }} field in you are using a custom template in case you used it to render each element seperately.

Upvotes: 0

Aastal
Aastal

Reputation: 350

Simply remove the 'current_password' field ?

$builder->remove('current_password');

If it don't work, you could use a PRE_SET_DATA event : Symfony doc

With this you could try to insert the current password in the form when it is init.

Good luck !

Upvotes: 2

Related Questions