简体   繁体   中英

Set password hidden in FOS UserBundle

I wanted to set the password field in registration form hidden since I don't need the password in my first step registration. The field is right now not showing since I have overridden the form, But still I could not submit the form because it says password should not be blank.

So I have tried this and its not working

class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('firstname')
        ->add('lastname')
        ->add('username')
        ->add('plainPassword',HiddenType::class)
        ->add('sex')
        ->add('email')
        ->add('city')
        ->add('zip_code')
        ->add('age');
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class'=>'AppBundle\Entity\User'
    ]);
}

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

}

Any help would be appreciated.

Thanks.

$builder
    ->add('password', HiddenType::class, [
        'required' => false,
        'validation_groups' => false
    ]);

You have a third parameter to provide options, you can use this to make it optional.

To modify the NotBlank constraint you could override the FOSUserBundle/Resources/config/validation.xml. Copy it to MyBundle/Resources/config/validation.xml and make changes you want.

So however managed to solve this for now with the help of OlivierC!

So I used life cycle callbacks to persist the data in the entity.

http://symfony.com/doc/current/doctrine/lifecycle_callbacks.html

Entity:

/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{

/**
 * @return string
 */
public function getPassword()
{
    return $this->password;
}

/**
 * @ORM\PrePersist()
 */
public function setPassword($password)
{
    $this->password = 'abcdef';
}}

Now it doesn't show as blank and for registration with password, I would need to update this field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM