2016-02-12 25 views
3

我最近纔開始使用Symfony(Symfony3),我正在將我的舊網站轉換爲使用Symfony。 目前與我的表單元素我添加了驗證錯誤消息元素,再加上幫助/信息元素(信息符號,顯示單擊文本)。Symfony FormBuilder - 額外元素的自定義選項?

<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處理驗證錯誤消息元素作爲標準,但我看不到一種輕鬆添加額外信息文本的方式。理想情況下,我希望將其作爲額外選項,然後傳遞給FormBuilder的「添加」方法。例如。

->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>' 
) 

這可能嗎?而不是直接在我的樹枝模板中添加它。

回答

0

我已經制定了自己的解決方案......

我加所需字段的'help'屬性。例如。

->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>' 
    ), 
    ) 
) 

我只好再覆蓋form_row塊添加我的幫助/信息框和內容。請注意使用「原始」過濾器 - 我需要這個,因爲我的幫助文本包含需要使用此過濾器呈現的html標記,因此標記不會被轉義。

{# \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 %} 

這讓信息框顯示正確。但是,它將原始文本保留在字段的「幫助」屬性中,因此我通過覆蓋widget_attributes塊並忽略輸出「幫助」屬性來刪除該文本。

{# \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

嘗試以下操作:

->add('username', TextType::class, 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>' 
) 

如果不能在此字段類型的工作,看看the following form extension

+0

_「選項」幫助「不存在。定義的選項是:..._ - 我會嘗試使用HelpExtension。 –

+0

這肯定是更簡單的替代方案(不涉及任何重寫默認字段) – chalasr

+0

我剛剛意識到這是我已經看過的相同的擴展,它不會安裝 - 項目顯示爲已棄用。 –

相關問題