Disabled form fields in custom form


(Rstone) #1

Hi there,

 

I've been trying to include a disabled form field in my custom form (Squiz Matrix v.4.18).

It is just a text input field which is pre-populated with the date via: %globals_date_d-m-Y% which I want to be visible to the user, but not be editable.

I've tried adding:

  • disabled="disabled"
  • disabled

Which both look like they work, but they don't submit the input value. Whereas adding:

  • readonly="readonly"
  • readonly

Does submit the input value, but doesn't seem to add the same nice visual and behavioural cues (from the browser) which indicate that the field cannot be edited (such as dimming the text, and not allowing the user to tab to the field etc).

 

Is the disabled attribute purposefully disallowed, or is this a bug (as disabled is a perfectly valid html attribute)?

 

Cheers,

Rob


(Joel Porgand) #2

It's not a bug. Disabled form fields will not submit a value - that's part of the html spec & the behaviour is dictated by your browser. 

 

http://www.w3.org/TR/html401/interact/forms.html#adef-disabled

 

If you wish to submit a a value, use a readonly field, and style it appropriately with css. 

 

It is also possible to hijack the submission event with javascript and add the value of the disabled field back into the submission before it's sent, but that is probably a silly way to do it.


(Rstone) #3

Thanks J.P!

 

I could have sworn I've used this in the past and still received the disabled input values.

RTFM strikes again.


(Tim Davison) #4

Set the form field 'extras' to hidden, then in your form's page contents bodycopy:

 

%question_field_x_y%

<input type="text" disabled value="%globals_date_d-m-Y%" />

 

That way the date still gets submitted, but the user sees a disabled input box (which does not get submitted).  


(Rstone) #5

Thanks Tim, good idea, but I think I'll go with readonly, and just style it to look disabled (as J.P suggested), rather than have the two fields.