如何在Twig中不显示密码字段中的标签

时间:2015-09-30 13:36:50

标签: php symfony twig

目前,我正在处理论坛的注册表格......

当我在树枝视图中使用{{ form_widget(form.plainPassword }}时,我得到:

<div>
    <label for="fos_user_registration_form_plainPassword_first" class="required">First</label>
    <input id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required" type="password">
</div>
<div>
    <label for="fos_user_registration_form_plainPassword_second" class="required">Second</label>
    <input id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required" type="password">
</div>

RegistrationFormType.php

class RegistrationFormType extends AbstractType
{
    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
            ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                //'first_options' => array('label' => 'form.password'),
                //'second_options' => array('label' => 'form.password_confirmation'),
                'invalid_message' => 'fos_user.password.mismatch',
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    // BC for SF < 2.7
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->configureOptions($resolver);
    }

    public function getName()
    {
        return 'bissap_user_registration';
    }
}

通常,当我只使用form_widget时,标签不会显示,但表单构建器配置标签仍然在表单中。

2 个答案:

答案 0 :(得分:3)

事实上,password字段是两个字段的组合:firstsecond

  

form_widget密码= 2 x(form_row输入)

使用

代替代码
{{ form_widget(form.plainPassword }}

只需更改

{{ form_widget(form.plainPassword.first }}
{{ form_widget(form.plainPassword.second }}

答案 1 :(得分:1)

删除标签:

$builder
            ->add('email', 'email', array('label' => false, 'translation_domain' => 'FOSUserBundle'))
            ->add('username', null, array('label' => false, 'translation_domain' => 'FOSUserBundle'))
            ->add('plainPassword', 'repeated', array(
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                //'first_options' => array('label' => false),
                //'second_options' => array('label' => false),
                'invalid_message' => 'fos_user.password.mismatch',
            ))
        ;

只需在表单构建器中添加array('label' => false)即可。