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

markus_petrux’s picture

Have you tried with the '#access' attribute of the form elements?

tordrup’s picture

Thanks for the reply!

I *think* so, but I'm struggling with the syntax. I tried this:


		$form['group_mygroup_b']['#access'] = FALSE;

...and the group was no longer visible in the form, but the "required" validation message still appeared.

Then I tried:


		$form['group_mygroup_b']['field_myrequired_field']['#access'] = FALSE;

And the required field was no longer visible in the form, but the "required" validation message still appeared.

Really puzzling!

markus_petrux’s picture

Maybe 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.

tordrup’s picture

Hey 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

markus_petrux’s picture

I think if you disable required fields, you'll need to provide default values altering their #value attribute.

capellic’s picture

@markus_petrux is right. Example code:

$form['group_contact_info']['field_profile_lname']['#access'] = FALSE;
$form['group_contact_info']['field_profile_lname']['#value'] = $form['group_contact_info']['field_profile_lname']['#default_value'];

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).

$form['field_read_privacy_policy']['#access'] = FALSE;
$form['field_read_privacy_policy']['#type'] = 'hidden';
$form['field_read_privacy_policy']['#value'] = $form['field_read_privacy_policy']['#default_value'];
capellic’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

WebJohn’s picture

Just 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';