3

Is this possible to make the whole form readonly with only one (or two) lines of code, as it is in PEAR/QuickForm?

I have the following code:

$form = $this->createFormBuilder($user) 
                ->add('username', \Symfony\Component\Form\Extension\Core\Type\TextType::class, array('label' => 'Login: '))
                ->add('personal_name', \Symfony\Component\Form\Extension\Core\Type\TextType::class, array('label' => 'Imię i nazwisko: '))
(...)

I know I can add "read_only" => true to every "add" function call, but is there a one-liner?

I also tried to use an AbstractType like this:

class Register extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('registration'),
            'readonly' => true //I also tried read_only, read-only, read only but to no avail  
        ));
    }   

The above code doesn't work, although when I replaced 'readonly' => true with 'disabled' => true it... worked. However I want it to be readonly.

What I'm trying to accomplish here is that I want to have ONE view (twig template, a form) for adding new entity, edit existing entity and displaying entity's details (this is where readonly would be useful). Maybe you have a better idea?

I know I can do this with Javascript, but maybe there is a Symfony - way to do that?

konrad_firm
  • 859
  • 1
  • 11
  • 28

1 Answers1

10

"read_only" was deprecated in Symfony 2.8, in favour of passing read only as an attribute instead. Try the following:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'validation_groups' => ['registration'],
        'attr' => ['readonly' => true],  
    ]);
}
hasumedic
  • 2,139
  • 12
  • 17