doushi3454
2016-02-12 17:16
浏览 217
已采纳

Symfony FormBuilder - 附加元素的自定义选项?

I've only recently started using Symfony (Symfony3) and I'm in the process of converting my old website to use Symfony. Currently with my form elements I have the addition of a validation error message element, plus a help/information element (an info symbol that shows the text on clicking).

<div id="info-username" class="additional-info">
    <p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
    <p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>
</div>

Symfony handles the validation error message element as standard but I can't see a way of easily adding extra info text. Ideally I would like this as an extra option I can then pass to the 'add' method of the FormBuilder. E.g.

->add('username', TextType::class, array(
    'info' => '<p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
<p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>'
)

Is that possible? Rather than adding it directly in my twig templates.

图片转代码服务由CSDN问答提供 功能建议

我最近才开始使用Symfony(Symfony3),而我正在将旧网站转换为 使用Symfony。 目前使用我的表单元素,我添加了一个验证错误消息元素,以及一个帮助/信息元素(一个显示单击时文本的信息符号)。

 <  code>&lt; div id =“info-username”class =“additional-info”&gt; 
&lt; p&gt;您的用户名将用作&lt; em&gt;唯一&lt; / em&gt;的方式 在您登录帐户时识别您的身份。 您仍然可以使用您的电子邮件地址登录,以防忘记用户名。&lt; / p&gt; 
&lt; p&gt;长度必须介于2到25个字符之间,并以字母开头。 允许使用以下字符:字母,数字,点,下划线,短划线。&lt; / p&gt; 
&lt; / div&gt; 
   
 
 

Symfony处理验证错误消息 元素作为标准,但我看不到轻松添加额外信息文本的方法。 理想情况下,我希望这是一个额外的选项,然后我可以传递给FormBuilder的'add'方法。 例如

   - &gt; add('username',TextType :: class,array(
'info'=&gt;'&lt; p&gt;您的用户名将用作 一种&lt; em&gt;唯一&lt; / em&gt;在您登录帐户时识别您的方式。您仍然可以使用您的电子邮件地址登录,以防您忘记用户名。&lt; / p&gt; 
&lt; p&gt;必须是 长度在2到25个字符之间,以字母开头。允许使用以下字符:字母,数字,点,下划线,短划线。&lt; / p&gt;'
)
   
 \  n 

这可能吗?而不是直接在我的树枝模板中添加它。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dtziv24262 2016-02-13 10:45
    最佳回答

    I've worked out my own solution...

    I added a 'help' attribute to the required fields. E.g.

    ->add('username', TextType::class, array(
        'attr' => array(
            'help' => '<p>Your username will be used as a way of <em>uniquely</em> identifying you when you log into your account. You can still log in using your email address in case you forget your username.</p>
    <p>It must be between 2-25 characters long and start with a letter. The following characters are permitted: letter, number, dot, underscore, dash.</p>'
        ),
        )
    )
    

    I then had to overwrite the form_row block to add my help/info box and contents. Note the use of the 'raw' filter - I needed this as my help text includes html tags that need to be rendered so using this filter the tags will not be escaped.

    {# \app\Resources\views\form\form_div_layout.html.twig #}
    {% block form_row %}
      {% spaceless %}
        <div class="form-group">
          {{ form_label(form) }}
          <div class="col-sm-10">
            {{ form_widget(form) }}
            {{ form_errors(form) }}
            {% for attrname,attrvalue in attr %}
              {% if attrname == 'help' %}
                <div id="info-{{ id }}" class="additional-info alert alert-info alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                  {{ attrvalue | raw }}
                </div>
              {% endif %}
            {% endfor %}
          </div>
        </div>
      {% endspaceless %}
    {% endblock form_row %}
    

    This got the info box to display correctly. However, it left the original text in a 'help' attribute of the field, so I removed that by overwriting the widget_attributes block and ignored outputting the 'help' attribute.

    {# \app\Resources\views\form\form_div_layout.html.twig #}
    {%- block widget_attributes -%}
        id="{{ id }}" name="{{ full_name }}"
        {%- if disabled %} disabled="disabled"{% endif -%}
        {%- if required %} required="required"{% endif -%}
        {%- for attrname, attrvalue in attr -%}
            {%- if attrname == 'help' -%}
              {# Prevent output of the help attribute #}
            {%- else -%}
              {{- " " -}}
              {%- if attrname in ['placeholder', 'title'] -%}
                  {{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
              {%- elseif attrvalue is same as(true) -%}
                  {{- attrname }}="{{ attrname }}"
              {%- elseif attrvalue is not same as(false) -%}
                  {{- attrname }}="{{ attrvalue }}"
              {%- endif -%}
            {%- endif -%}
        {%- endfor -%}
    {%- endblock widget_attributes -%}
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题