Symfony read-only form

How to display read-only fields using a symfony form component?

Here's how I try to do this to no avail:

Symfony 2

 $builder ->add('descripcion', 'text', array( 'read_only' =>'true' )); } 

Symfony 3

 $builder ->add('descripcion', TextType::class, array( 'read_only' => 'true' )); } 
+11
source share
13 answers

The answers provided end with this exception on Symfony 3 :

PHP Exception Symfony \ Component \ OptionsResolver \ Exception \ UndefinedOptionsException is not displayed: "The read_only option does not exist.

The proper way to do this is to use the attr property in the field:

 ->add('descripcion', TextareaType::class, array( 'attr' => array( 'readonly' => true, ), )); 
+20
source

You specified your read-only attribute in a string, it should be logical.

remove quotes around true

like this:

 ->add('descripcion','text',array('read_only' => true)) 

true without quotes.

+9
source

I believe that the only safe way to present a form field as read-only, and to prevent your form from accepting a new value in the request, is as follows.

 $builder->add( 'description', TextType::class, ['disabled' => true] ); 

Another suggestion to use ['attr' => ['readonly' => true]] or ['attr' => ['disabled' => true]] will make you vulnerable to fake requests.

Both last parameters will set the readonly only or disabled attributes in the field, but your form will still accept a new value for this field if it is included in the request.

Only the first option above will disable the form field, and also will not allow your Form to accept a new value for the field in the request.

I tested this with Symfony Form 3.4. I do not know if 4 behaves the same.

+6
source

read_only is deprecated since Symfony 2.8. So use readonly instead. And provide a boolean for this attribute

 ->add('','text',array('readonly' => true)) 
+5
source

Update. Starting with Symfony 3.0, readonly must be set to the attr parameter. http://symfony.com/doc/2.8/reference/forms/types/form.html#read-only

Instead, you can also use the disabled option.

+4
source

Symfony 4 allows you to use only the “disabled” option in the form field. But this is something else that is read-only.

  • Disabled - the user cannot edit the field, and its value is NOT transmitted during form submission.
  • Read-only - the user cannot edit the field, but its value is transmitted during the form submission.

The only solution I found for read-only:

 ->add('fieldname', TextType::class, [ 'label' => false, 'attr'=> [ 'readonly' => true ] ]) 
+3
source

I recommend using the disabled option, because any submitted value will be ignored according to the documents: https://symfony.com/doc/current/reference/forms/types/text.html#disabled

 $builder->add('descripcion', TextType::class, [ 'disabled' => 'true', ]); 
+1
source

If familia and proofedor are relations to another object, I think they should not be textual. Try to invalidate their types or change the type of entity and see if it worked.

0
source

Let me add something that the other answers did not help with. Processing is like a field, but "setting" to disable the publication can work in many cases. However, at least it is difficult to do in some formats that completely prevent the release (i.e. render as a label).

How to solve this? What I did was define the field as HiddenType, and in the template render using {{ form.vars.value.myfield }} or {{ item.myfield }} , taking "item" as an object object enclosed in all you can think of like any other HTML element.

0
source

readonly for read_only readonly and not read_only . You should make this option in attr as follows:

 ->add('', TextType::class, array('attr'=> array('readonly' => true))) 
0
source

Another solution might be:

 ->add('value', TextType::class, ['disabled' => true]): 

Taken from: http://symfony.com/doc/current/reference/forms/types/text.html#disabled

0
source

Only the "disabled" option does not cause an error

 $builder ->add('descripcion', TextType::class, array( 'disabled' => 'true' )); } 
0
source

For a disabled object type field, it works fine

 ->add('organizacion', EntityType::class, array( 'class' => 'AppBundle:Organizacion', 'label' => 'Institución/Organización', 'choice_label' => 'nombre', 'disabled' => true )) 
0
source

All Articles