Hi!
I'm overriding node_edit, and passing an argument into the edit form, to show and hide certain fields.
Here's my override in template.php:
/**
* Theme override for node edit form.
*
*/
function mytheme_mycontenttype_node_form($form) {
if (arg(3) == 'field_note') {
$form['title']['#type'] = 'hidden';
$form['body_field']['#type'] = 'value';
$form['group_myfield']['#type'] = 'value';
$form['group_mygroup']['#type'] = 'value';
$form['group_mygroup_b']['#type'] = 'value';
} else {
$form['field_note']['#type'] = 'value';
}
return drupal_render($form);
}
The problem is, "group_mygroup_b" contains required fields. When I switch its #type to "value" or "hidden", the user submits the form and gets a "field x is required" message (even when the field contains a value).
'title' is required (obviously), and when I switch it to "hidden" its value persists through the override and therefore passes validation.
It's almost as if switching the required CCK fields to #type="hidden" or #type="value" removes the field's value.
I've tried every combination of "hidden" and "value", on both the fieldgroup and the required fields that it contains, but get the same result.
Any suggestions would be greatly appreciated!
All the best,
Dave
Comments
Comment #1
markus_petrux commentedHave you tried with the '#access' attribute of the form elements?
Comment #2
tordrup commentedThanks for the reply!
I *think* so, but I'm struggling with the syntax. I tried this:
...and the group was no longer visible in the form, but the "required" validation message still appeared.
Then I tried:
And the required field was no longer visible in the form, but the "required" validation message still appeared.
Really puzzling!
Comment #3
markus_petrux commentedMaybe you need to provide defaults using the '#value' attribute of the form elements.
Otherwise, you may need to alter the '#required' attribute of the elements using a recursive function from hook_form_alter() + after_build callback. See this for an example: Code snippet: How to set the disabled attribute of a CCK field.
Comment #4
tordrup commentedHey markus_petrux,
I tried that approach, and got the fields to disable (which is great) -- but I'm still getting the required message, even when I set the default_value to a static string.
What about #post? Would it be worth trying to modify that directly?
Thanks again for your input,
Dave
Comment #5
markus_petrux commentedI think if you disable required fields, you'll need to provide default values altering their #value attribute.
Comment #6
capellic@markus_petrux is right. Example code:
Here's example code for a single checkbox CCK field. If you don't set to hidden it still expects the data in an array).
Comment #7
capellicComment #9
WebJohn commentedJust as a note - I had the same issue with a slightly different node form, and to fix it I had to set both:
$form...['#value']['value'] = 'yourvalue';
$form...['value']['#value'] = 'yourvalue';