1

I am altering the "Add node" form for one of my types so that I can set the title and disable the text box. The (shortened) code looks like this:

function mytheme_form_node_form_alter(&$form)
{
    $form['title']['#default_value'] = 'Some value';
    $form['title']['#disabled'] = true;
}

On the node form, I have a field with "Unlimited values" where a user can click "Add another item" to add another row to the items grid on the form.

The problem: When I click this button and the AJAX request happens, it adds the row to the table as usual. But when I save the form, Drupal thinks the Title is empty, and throws a validation error back. The title is still set and greyed out after the AJAX request happens (like it should be), but it seems to "unset" itself somehow behind the scenes when the AJAX request happens, because when the form gets saved, the title gets cleared and the validation error occurs.

I hope I made that clear... so does anyone know why the AJAX related to unlimited fields might interfere with theme_form_X_form_alter?

3
  • 1
    Have you actually verified that you are able to save the node before you have clicked on the 'Add another' button? Because I suspect that setting the "disabled" attribute is the wrong way to go; in normal form processing, a disabled element is not sent in the request. Instead, using #access set to FALSE might work.
    – kekkis
    Commented Oct 2, 2012 at 19:30
  • Yes, I can save it correctly if I don't invoke the AJAX. #access is not what I want, this is from the Drupal reference: "Description: Whether the element is accessible or not; when FALSE, the element is not rendered and the user submitted value is not taken into consideration." This is not the desired behavior.
    – qJake
    Commented Oct 3, 2012 at 23:07
  • @spidex can you paste your complete code here so that it will be debugged Commented Oct 10, 2012 at 7:44

1 Answer 1

1
function mytheme_form_node_form_alter(&$form){
    $form['title']['#default_value'] = 'Some value';
    $form['title']['#value'] = $form['title']['#default_value'];
    $form['title']['#disabled'] = true;
}

This should solve the problem. In HTML, disabled form elements' value will not be sent back to the server so server gets nothing back, which result in a form error if the value is required. Storing value in #value overrides any browser input which is a must (for security purposes) when you disable a form element and expect its value back.

4
  • lullabot.com/articles/modifying-forms-drupal-5-and-6 very strongly suggests that hook_form_FORM_ID_alter can only be implemented in a module, and my testing would seem to confirm that.
    – kekkis
    Commented Oct 4, 2012 at 4:20
  • 2
    Quoted from question "The title is still set and greyed out after the AJAX request happens (like it should be)" so I think theme hooks worked for him. Plus, I never had a problem with form alters in template.php. That is not the smartest way but sometimes we have to rely on template.php.
    – AKS
    Commented Oct 4, 2012 at 11:10
  • This was a great suggestion, unfortunately, it didn't work. It still thinks the title field is empty after invoking an AJAX request. I don't think form_X_form_alter is executed during an AJAX request, hence why the values are being cleared. Is there an AJAX hook I need to hook into in order to make this work?
    – qJake
    Commented Oct 9, 2012 at 1:32
  • Can you try this on a module hook ? I have done this on a regular form and it worked that's why I was sure about this would work.
    – AKS
    Commented Oct 9, 2012 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.