FOSUser Bundle 为表单元素添加PlaceHolder[英] FOSUser Bundle Adding PlaceHolder to form element

本文是小编为大家收集整理的关于FOSUser Bundle 为表单元素添加PlaceHolder的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我明白,当您覆盖Fosuser捆绑包时,您会遵循此结构.

{{ form_widget(form.plainPassword.first, { 'attr': {'class': 'myformclass'} }) }}

我还想将占位符选项添加到我的表单元素中.我该怎么做?

所以看起来像这样;

<input class="myformclass" placeholder="Password" type="password" id="password" name="_password" required="required" />

推荐答案

{{ form_widget(form.plainPassword.first, { 'attr': {'class': 'myformclass', 'placeholder': 'Password', 'id': password'}}) }}

字段名称取决于您的表格名称

它希望以fosuser捆绑寄存器表格

命名
fos_user_registration_form[plainPassword][password]

ID将为

fos_user_registration_form_plainPassword_password

您可以像我的示例中写的那样覆盖" id"

所需和名称选项在表单类中定义

其他推荐答案

您可以使用占位符属性.

方法1:twig

{{ form_widget(form.plainPassword.first, { 'attr': {'placeholder': 'enter your password'} }) }}

方法2:形式建筑

$builder
    ->add('plainPassword', null , array(
        'attr'=> 
            array('placeholder'=>'enter your password')
        )
    );

本文地址:https://www.itbaoku.cn/post/2090830.html

问题描述

I understand that when you are overriding FOSUser Bundle Forms, you follow this structure;

{{ form_widget(form.plainPassword.first, { 'attr': {'class': 'myformclass'} }) }}

I also want to add PlaceHolder option to my form element.. How can i do that?

so it will look like something like this;

<input class="myformclass" placeholder="Password" type="password" id="password" name="_password" required="required" />

推荐答案

{{ form_widget(form.plainPassword.first, { 'attr': {'class': 'myformclass', 'placeholder': 'Password', 'id': password'}}) }}

Field name depends on your form name

it will like be named this for fosuser bundle register form

fos_user_registration_form[plainPassword][password]

the id will be

fos_user_registration_form_plainPassword_password

You can override "id" like written in my example

Required and name options are defined in the form class

其他推荐答案

You can use the placeholder attribute.

METHOD 1: Twig

{{ form_widget(form.plainPassword.first, { 'attr': {'placeholder': 'enter your password'} }) }}

METHOD 2: Form Builder

$builder
    ->add('plainPassword', null , array(
        'attr'=> 
            array('placeholder'=>'enter your password')
        )
    );