Hello,
I was wondering if someone could help me learn about and decide if I should use unset <?php unset($form['field_nationality']); ?> or make #access FALSE to restrict the field to specific users. I would like only allow the field to be shown and used for role A on the node form and not for role B.
I hope someone can direct me.
Thanks!
Comments
Comment #1
yched commentedAs a general rule across drupal, use #access rather than brute-force unset. Other code in other modules might rely in the element being present in the array.
Comment #2
YK85 commentedIn my custom module I used
$form['field_example']['#access'] = FALSE;but the user still got the "Field Example is required" error message when trying to submit the form. Usingunset($form['field_example']);made the field not show and the error message did not show.I would like to use the recommended #access method but why is the field required error message still showing?
Thank you
Comment #3
YK85 commentedCould anyone kindly help with this issue I am facing?
I used
$form['field_example']['#access'] = FALSE;in template.php for roleB only, but I still get the "Field Example is required" error when user with roleB saves the node.Comment #4
zeezhao commentedAlso have a look at this page for more information: http://drupal.org/node/357328
Comment #5
karens commentedIf you are not allowing someone access to a required field, you will need to provide a value for that field. Without trying it I'm not sure if you want to set #value or #default_value in this case, but one or the other will work.
You should certainly not unset a required field ever, or your database will be corrupted by missing required values.
Comment #6
YK85 commentedHi,
markus_petrux mentions at http://drupal.org/node/357328#comment-1443156 that a different approach is needed to alter #required
If I make the field not required in the settings, but want to make it required for just one specific role, can someone help me with the code to achieve this?
roleA << all users: field will not be required as per settings, nothing saved in database
roleB << specific users: want to set field to required with code, value saved in database
I hope someone may have the time to help.
Thank you
Comment #7
zeezhao commentedI have used exactly the same method to do this before. What you need is a function to apply to your condition, and call it in the after_build just like is done in the examples in the link above. Function will look like this:
function yourmodule_fix_required(&$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
// Recurse through all children elements.
yourmodule_fix_required($elements[$key]);
}
}
$elements['#required'] = TRUE;
}
Comment #8
YK85 commentedHi, I appreciate the very quick reply!
I'm really new to programming and was wondering if I want to do the same for field_a, field_b, field_c...field_q do I need to type the code for each field or is there a better way to make it compact?
Thanks!
Comment #9
zeezhao commentedWithout knowing the specifics of your code, unless there is a way to read your fields via a loop, you'll have to do each one.
Remember that you probably can't set the same field to false and also required... unless for different roles seeing the fields.
Comment #10
YK85 commentedHi again,
The below code is from the link in #4 + code in #7. It seems to work for field_myfield but I would somehow like to make it work for more than one field (make 5 fields required) for users with role_a
Comment #12
fnikola commentedcan this code be placed in your template.php?