I have a content type with required field in it. If the field is disabled then Drupal won't verify it. (it is being verified if not disabled). How can I verify that the field has a value even if it's disabled?

More details:

For some of the users I set the value by Prepopulate and disable the field (by implementing the hook hook_form_alter).

I still want to prevent the user from messing with the URL. For example: he can take the "new content" link I've created for him that looks like:


and remove the URL query:


I thought that if he would do it, he won't be able to save the form because of the required field (which is disabled) - but Drupal won't verify it.


According to the API docs of _form_builder_handle_input_element disabled fields are ignored, you can try using #access instead.

You can try adding your own handler after that of prepopulate and check if a (valid) value is entered, if not redirect the user or display a message

function MY_MODULE_form_alter(&$form, $form_state, $form_id) {
  $form['#after_build'][] = 'MY_MODULE_prepopulate_after_build';

function MY_MODULE_prepopulate_after_build($form, &$form_state) {
  // check #value
  if (empty($form['field_to_set'][0]['#value'])) {
    drupal_set_message ('You altered the URL', 'error');
    drupal_goto ();
  // if #value is set, set #default_value as well
  if (empty($form['field_to_set']['und']['#default_value'])) {
    $form['field_to_set']['und']['#default_value'] = $form['field_to_set']['und']['#value']
  return $form;
  • I didn't got it. do you mean that I'll disable the field by myself or that I;ll verify the form by myself? also, please explain the code above (I'm novice with Drupal development). Thanks! – Tibo May 24 '12 at 7:27
  • You can do both, it depends on if you want the user to see the field or not. #disabled still shows the field why #access hides it completely. How are you disabling the field right now? – Attiks May 24 '12 at 7:29
  • For the code you have to create your own module, there's a tutorial at drupal.org/node/1074360 – Attiks May 24 '12 at 7:30
  • currently I disable the field in my own module, by: $form['field_name']['und']['#disabled'] = TRUE; – Tibo May 24 '12 at 7:58
  • in the same module you can add the code above, or maybe check in the same function you're using now for #value and/or #default_value – Attiks May 24 '12 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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