Search code examples
drupaldrupal-6cckdrupal-fapi

How do I hide a required CCK Nodereference input widget in #after_build?


Once the node has been saved once I'd like to block users from editing a required nodereference field. I tried changing the type to 'nodereference_hidden' like this but then submission won't validate: The field is required.

... So I ended up un-checking 'required' in the field configuration UI, and hiding it like so:

$form['field_my_nodereference_field'][0]['#type'] = 'nodereference_value';

Since it's passed as value there's no reason for it to be lost, still I'd have felt safer keeping the field 'required'.


Solution

  • The drupal 'required' validation does some funny stuff (dropping css, resetting multi-stage forms, and more!), luckily you can add your own hook_validate function that checks for the value, makes sure it's sane, and so forth. Add a red asterisk and call it a day.