简体   繁体   English

设置隐藏在FOS UserBundle中的密码

[英]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. 要修改NotBlank约束,您可以覆盖FOSUserBundle / Resources / config / validation.xml。 Copy it to MyBundle/Resources/config/validation.xml and make changes you want. 将其复制到MyBundle / Resources / config / validation.xml并进行所需的更改。

So however managed to solve this for now with the help of OlivierC! 因此,现在借助OlivierC设法解决了这一问题!

So I used life cycle callbacks to persist the data in the entity. 因此,我使用了生命周期回调将数据持久保存在实体中。

http://symfony.com/doc/current/doctrine/lifecycle_callbacks.html 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. 现在它不会显示为空白,并且要使用密码注册,我需要更新此字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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