Select2: Readonly Support in v4.0

Created on 21 May 2015  路  21Comments  路  Source: select2/select2

Hi,

i'm currently migrating my app to select 2 4.0 and I can't figure it out how to implement the readonly.

In the 3.*, the function

$('select').select2('readonly',true);

worked very well.
The behavior protects the user from editing the select and the form element is sent through the form posting.
The disabled property works well but it prevents the form element to be posted. (it's a normal behavior)

With the 4.0 version, the readonly is not handled at all.

$('select').prop('readonly',true);

Does nothing.

I think it will be an interesting feature to have.
I've made a fiddle of this :
http://jsfiddle.net/skxad701/2/

The behavior will be the same as the disabled one , except it can be sent.

I've made a workaround to remove the disabled property before submitting my form.

Do you think it's something that can be planned to be added in the future version ?

enhancement help wanted

Most helpful comment

Why would it be any different from all the other form input fields? All the other readonly input fields send data but you can't edit, but no one is worrying about validating them. Why not just remove readonly from everything then?
You shouldn't worry if it's being validated, that's the developers job to do it, or not. Let them decide what makes most sense.

I thought select2 was to enhance <select> and fix the flaws it has.

How many people have to use disable select, hidden input and javascript to fix that one thing.

All 21 comments

As mentioned in the 4.0 release notes, there is no way to make a <select> read-only and because 4.0 supports the <select> as the primary data source, this is going to be pretty difficult to implement.

Using a read-only state to prevent users from modifying data that needs to be sent to the server hints at the idea that you _might not_ be validating data on the server side. It's not a wise choice to depend on the client to not modify data that is sent, and a read-only select doesn't actually make that much sense (which is why we used to disable it).


With that in mind, I don't plan on somehow implementing this soon. As part of the ongoing attempt to cut down on the number of (soon-to-be) inactive tickets on GitHub, I'm going to close this off.

Why would it be any different from all the other form input fields? All the other readonly input fields send data but you can't edit, but no one is worrying about validating them. Why not just remove readonly from everything then?
You shouldn't worry if it's being validated, that's the developers job to do it, or not. Let them decide what makes most sense.

I thought select2 was to enhance <select> and fix the flaws it has.

How many people have to use disable select, hidden input and javascript to fix that one thing.

Just created a workaround for me. may be useful for anybody else.
https://jsfiddle.net/ujdbcy3d/14/

You can fix it by using css. This is a scss file sample that worked for me using the bootstrap theme.

select[readonly].select2 + .select2-container {
  pointer-events: none;
  touch-action: none;

  .select2-selection {
    background: #eee;
    box-shadow: none;
  }

  .select2-selection__arrow,
  .select2-selection__clear {
    display: none;
  }
}

I did what leonardofalk did.

One caveat: when using jquery to dynamically change readonly you'll have to use attr rather than prop, as at least some browsers ignore the readonly prop on selects.

also, the idea that because it's true for your use case that readonly is not useful/desired that therefore it ought not be useful/desired for anyone is rather prescriptivist.

I'm going to remind everyone that the native <select> doesn't support readonly. And since Select2 is designed to enhance (read: not replace) a standard <select>, we also face that same limitation.

Another option that suited my needs is to use disabled, then using JS on form submit, enable the input. The user can not interact with the input, but the values are still passed.

For me worked what leonardofalk did but a little changed. My select2 Version is 4.03

select[readonly].select2-hidden-accessible + .select2-container {
  pointer-events: none;
  touch-action: none;

  .select2-selection {
    background: #eee;
    box-shadow: none;
  }

  .select2-selection__arrow,
  .select2-selection__clear {
    display: none;
  }
}

I also used what transputec did to make a working example here

I think the fix provided by leonardofalk and updated by nikitul should be pushed into the Select2 main branch. I think this would be a good enhancement to the standard