5

I have a field that is currently marked as required. However, I want this to be required for all roles but the Admin. I've made a module that implements hook_form_alter(), and I have been able to remove the required status for all fields but the Date fields. Any idea what settings I need to change?

Also, I've tried altering it in hook_form_alter(), as well as doing it in the '#after_build'. I had no luck there either.

  • 2
    This is a good question. The intuitive form logic does not apply. I believe this is because the date system checks back in with the main content definition to see if there should be a "to" date, and whether or not that too is required. The answer is likely in the internals of date.module. – Josh Koenig Mar 26 '11 at 22:58
3

This is most probably because you don't just use a simple datefield, but one provided by e.g. the Date Module.

You should make sure that the module implementing your custom hook_form_alter runs last. That way, no other module can change it back to "required" again.

Furthermore, some modules implement a different "required" allthogether. E.g. the business-logic "if start-date is set then end-date is required too" cannot be implemented with simple "required" flags but will fire on the hook_validations for that form.

To override that, you will need to either exclude certain validations from running, by removing them from the element_validate or from the entire forms #validate.

You might want to replace these original validations with your own copies, to be sure you don't change ugly behaviour.

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.