Download & Extend

Using FAPI #disabled on some element types causes values to be ignored

Project:Drupal core
Version:6.x-dev
Component:forms system
Category:bug report
Priority:critical
Assigned:Unassigned
Status:active

Issue Summary

I don't know if this exists in 7.x but it does in 6.10. However, I suspect that the issue hasn't changed between the two since I couldn't find any relative issue. I have installed the wikitools on a version 6.10 release and do not allow most users to change the title. Wikitools enables this by adding a #disabled to the title field in the node edit form which worked perfectly in version 5.3. However, in 6.10 and probably 7.x the value doesn't carry to the validation and the value becomes lost so the user sees a "Title field is required" error message. I will open a sister issue to wikitools as well to make sure their maintainers are aware.

Comments

#1

#2

Version:6.10» 7.x-dev

Bumping to D7.

This is due to the fact that some value callbacks (form_type_*_value() functions) returns a value even when $edit is NULL (which is the case when a disabled element is posted).

In the case at hand:

<?php
function form_type_textfield_value($form, $edit = FALSE) {
  if (
$edit !== FALSE) {
   
// Equate $edit to the form value to ensure it's marked for
    // validation.
   
return str_replace(array("\r", "\n"), '', $edit);
  }
}
?>

Here str_replace() will cast the NULL $edit value to an empty string, leading to that bug.

#3

Title:Using FAPI #disabled on causes values to be ignored.» Using FAPI #disabled on some element types causes values to be ignored

#4

Thanks, Damien. What do you suggest for a fix? Maybe

<?php
function form_type_textfield_value($form, $edit = FALSE) {
  if (
$edit !== FALSE && $edit !== NULL) {
   
// Equate $edit to the form value to ensure it's marked for
    // validation.
   
return str_replace(array("\r", "\n"), '', $edit);
  }
}
?>

#5

Maybe this is better?

<?php
function form_type_textfield_value($form, $edit = NULL) {
  if (
$edit !== NULL) {
   
// Equate $edit to the form value to ensure it's marked for
    // validation.
   
return str_replace(array("\r", "\n"), '', $edit);
  }
}
?>

#6

Status:active» needs review

Actually, this is already fixed in Drupal 7.

Here is a test.

AttachmentSizeStatusTest resultOperations
410926-default-form-values.patch4.93 KBIdlePassed on all environments.View details | Re-test

#7

Status:needs review» reviewed & tested by the community

Good!

#8

Status:reviewed & tested by the community» fixed

Excellent! Go, DamZ, GO! :)

Committed to HEAD.

#9

Version:7.x-dev» 6.x-dev
Status:fixed» active

This is still a bug in Drupal 6.

#10

I think we had duplicates for this, so marking the tests for backport does not seem to make that much sense. I found #426056: Server-side enforcement of #disabled is inconsistent and #227966: Use default values to #disabled form fields with a quick search.

#11

This one seems working for me in D6 and makes the value available after "submit", nevertheless the #disabled is TRUE:

<?php
   
// TODO: '#default_value' is currently broken with '#disabled', see #410926.
   
$form['googleanalytics_custom_var']['slots'][$i]['slot'] = array(
     
//'#attributes' => array('readonly' => 'readonly'),
      //'#default_value' => $i,
     
'#description' => t('Slot number'),
     
'#disabled' => TRUE,
     
'#size' => 1,
     
'#type' => 'textfield',
     
'#value' => $i,
    );
?>

As I'm not mister FAPI - I hope this code is correct and will not break after this bug may be fix here...