5

I have functionality set up where an admin can create a user with a content profile. The content profile has a required field but I want the user himself to fill it out. How can I disable the required-ness of that field when the admin is creating the user so he can submit the form. I tried setting #required to 0 or unsetting it, but that does nothing.

function mymodule_form_contenttype_node_form_alter(&$form, &$form_state) {
        $form['field_reviewer_instruments']['#access'] = FALSE;
        $form['field_reviewer_instruments']['#required'] = FALSE;
}
4
  • was this implemented in hook_form_alter()? It should be as simple as implementing hook_form_alter() and setting $form['field']['#required'] = FALSE; Then again, I think unsetting the attribute should work as well. Are you sure the hook is being called correctly? Maybe if we see the code you're using we can spot the problem.
    – jordojuice
    Sep 15, 2011 at 20:56
  • There ya go. As far as I know it's just a basic hook_form_FORM_ID_alter but it doesn't seem to care that I change the #required.
    – Joren
    Sep 15, 2011 at 21:04
  • Very interesting. I'm scratching my head definitely - the code is obviously fine - but I swore I saw another question just like it recently! I'll see what I can find out. Is #access not working either? Is the hook implementation displaying test messages properly?
    – jordojuice
    Sep 15, 2011 at 21:49
  • Yes and also yes.
    – Joren
    Sep 15, 2011 at 22:02

2 Answers 2

1

This is most likely because of the ordering. THe module which is altering the form and setting the required element is being run after yours.

In the system table set your module weight to be higher than the module which is affecting the form.

1

Another answer mentioned the ordering which is probably right. Another way to handle this is to use the #after_build property and do it in the function defined there.

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.