Search code examples
phpjquerysymfonydisabled-input

Symfony 3 undisabled select not submitting


I have a read only select defined by the formbuilder:

    ->add('sampledBy', EntityType::class, array(
        'class' => 'AppBundle:FOSUser',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('u')
                ->orderBy('u.cn', 'ASC');
        },
        'choice_label' => 'cn',
        'disabled' => true
    ))

Which I use JQuery to update from another field in my form. Following this answer, I've set the select up so that it can't be changed by the user:

*Set it to disabled on create.

*Before submit set it to disabled = false:

 $(form).on('submit', function() {
     $("select[id$='sampledBy']").prop('disabled', false);
 });

Now, on submit I see the select field change to disabled = false (the colour changes back to white and it's clickable while the form submits. However, the data isn't saved.

Has anyone seen similar behavior? The JQuery appears to work correctly, so I'm wondering if it's a Symfony specific issue.


Solution

  • The state is not changed in the browser even if the html said so.

    To by pass this, I changed the field to readonly through javascript and changed the CSS to make it look like it's disabled.

    ->add('sampledBy', EntityType::class, array(
            'class' => 'AppBundle:FOSUser',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.cn', 'ASC');
            },
            'choice_label' => 'cn',
            'disabled' => false
        ))
    

    in the css

    .readonly{
    background-color: transparent !important;
      border : 0px !important;
        box-shadow: none;
    }
    .disabled{
    background-color: transparent !important;
      border : 0px !important;
        box-shadow: none;
    }