PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 11, 2022

[FIXED] How to add a WAI-compliant label to a Symfony form

 May 11, 2022     symfony, symfony-forms, wai-aria     No comments   

Issue

WAI validation requires label with for attribute associated with every form inputs.

How can I add label on form from this code?

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nom', 'text', array(
            'required' => false,
            'attr' => array(
                'placeholder' => 'Nom, Prénom', ),
         ))
    ;
}

Things like this do not work:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nom', 'text', array(
           'label' => 'whatever',
            'required' => false,
            'attr' => array(
                'placeholder' => 'Nom, Prénom', ),
         ))
    ;
}

Solution

You need to add {{ form_label(form.nom) }} separately if you are not rendering the whole form.

Take a look here

Do something like this in your code:

{{ form_start(form, {'method': 'POST'}) }} 
{{ form_label(form.nom) }}
{{ form_widget(form.nom) }}
{{ form_end(form) }}

and it should work.



Answered By - Dipen Shah
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing