开发者

Using tabindex on twig generated forms

开发者 https://www.devze.com 2023-03-25 07:44 出处:网络
I am trying to incorporate tabindex on my form to give users freedom for not using the mouse. The problem is that I am using Twig (http://www.twig-project.org/) to create the form template for the pag

I am trying to incorporate tabindex on my form to give users freedom for not using the mouse. The problem is that I am using Twig (http://www.twig-project.org/) to create the form template for the page. How do I set the attribute of a twig-generated form input element?

              <div class="LeftSide">
                <div class="Wrapper">
                  {{ form_label(mehForm.amount, "Amount") }}
                </div>
              </div>
              <div class="RightSide">
                <div class="Wrapper Tiny">
                  {{ form_widget(mehForm.amount) }}
                  <label class="ErrorContainer"></label>
                  <div class="clear"></div>
                </div>
              </div>

When rendered, the line of {{ form_widget(mehForm.amount) }} will get changed by Twig into:

<input type="text" id="meh_amount" name="meh[amount]" required="required" value="">

The goal is to command Twig to add one more attribute which is tabindex:

<input type="text" id="meh_amount" 开发者_StackOverflow中文版name="meh[amount]" required="required" tabindex=1 value="">

Thank you


It's been a while since you asked the question, but since it was the top site on Google when I searched for something similar, figure I'd answer the question so other's have the answer.

Since you're already using the form_widget() function to generate the widget, you can easily just add the tabindex by setting the attr option like so:

{{ form_widget(mehForm.amount, { 'attr': {'tabindex': '1'} }) }}
0

精彩评论

暂无评论...
验证码 换一张
取 消