I have found the code that I need to include in my node edit form, so that one of two fields is required on a form, from http://drupal.org/node/355264.
However, I need the code to function AFTER the node has been submitted. I.e. the message about "either x or y needs to be filled" to show up after the node has been submitted, along with the other messages about reuired fields.
Currently it shows up before.
Where do I put the code??
thanks

Comments

nevets’s picture

You want to add a validation callback not one on submit.

sylvie_n’s picture

thanks for your response nevets.
how would i do that?
any code i can copy or a place i can read up about it?
i'm not a programmer, so i just kind of muddle my way through...learning process :)

sylvie_n’s picture

So far i have combined code from http://drupal.org/node/486340 and http://drupal.org/node/355264 and combined the two to come up with this:

function bluemarine_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'node_form'){
    $form['#submit'][] = 'mymodule_mysubmit';
  }
}

function mymodule_mysubmit() {
  // Grab variables.
$email = $form_values['submitted_tree']['email_address'];
$phone = $form_values['submitted_tree']['phone_number'];

// Check for values.
if ($email == "" && $phone == "") {
  form_set_error('submitted][email_address', t('E-mail Addresses or Phone Number must be given.'));
  form_set_error('submitted][phone_number', '');
}
}

I put the above in my template.php file, but no joy! :(
You say that i shouldn;t be using $form['#submit'][] though, right?
what should i use instead.
thanks

nevets’s picture

Two things

1) I would use '#validate' instead of '#submit', validate handlers if they post errors (form_set_error) keep the form from even submitting.

2) I think (not positive) the hook_form_alter() needs to be implemented in a module (not template.php)

sylvie_n’s picture

Hi Nevets.
Thanks for the help, but unfortunately still no luck.
I created a module, with the following:

// $Id: customsite.module

/**
* @file
* Custom functions for this site.
*/

function required_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'node_form'){
    $form['#validate'][] = 'required_mysubmit';
  }
}

function required_mysubmit() {
  
// Grab variables.
$email = $form_values['submitted_tree']['email_address'];
$phone = $form_values['submitted_tree']['phone_number'];

// Check for values.
if ($email == "" && $phone == "") {
  form_set_error('submitted][email_address', t('E-mail Addresses or Phone Number must be given.'));
  form_set_error('submitted][phone_number', '');
}

}

But it just doesn't change a thing. A message saying "Description field is required." comes up, but nothing which says "E-mail Addresses or Phone Number must be given."
Does this seem ok as a module to you? I have created it as a weight of 10.
Thank you v.much for your help.

nevets’s picture

A couple things, your function does not have the correct arguments defined and checks values incorrectly. This code comes from the Form API reference.

  function test_form_validate($form, &$form_state) {
  if ($form_state['values']['name'] == '') {
    form_set_error('name', t('You must select a name for this group of settings.'));
  }
} 

Notice the arguments and the use of $form_state['values']['name'] to check the value. (your code comes from a webform validation function).

sylvie_n’s picture

Thanks for your reply nevets.
I have tried a few variations of the following:

// $Id: customsite.module

/**
* @file
* Custom functions for this site.
*/

function required_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'node_form'){
    $form['#validate'][] = 'required_mysubmit';
  }
}

function required_mysubmit($form, &$form_state) {
  if (($form_state['values']['submitted_tree']['email_address']== '')&&($form_state['values']['submitted_tree']['phone_number']== '')) {
      form_set_error('submitted', t('E-mail Addresses or Phone Number must be given.'));

  }
} 
 

But, no luck. I have tried cutting out the first function, to see if thats the problem - but still doesn't work.
Am I at all on the right track...or just completely off it?!
Not sure if i used what you said in the right direction?!

nevets’s picture

At the start of required_mysubmit add this line

drupal_set_message('Form fields: ' . implode(', ',  array_keys($form_state['values'])));

and when you submit the form all the form field names (keys) should print in the message are

There will not be a 'submitted_tree' element as that is part of webforms. You fields will be something like 'email_address' or if CCK fields, something like 'field_email_address'. So assuming CCK fields, your code should look something like

function required_mysubmit($form, &$form_state) {
  if (($form_state['values']['field_email_address']== '')&&($form_state['values']['field_phone_number']== '')) {
      form_set_error('submitted', t('E-mail Addresses or Phone Number must be given.'));

  }
} 

Also, the field name passed to form_set_error should be a valid form field name.

sylvie_n’s picture

hi nevets,
yeah - thanks for that - i forgot to put in field.
However, still no luck. The problem lies in the fact that the second function isn't being called. When i put in
drupal_set_message('Form fields: ' . implode(', ', array_keys($form_state['values'])));
nothing is printed on the form.

Hence, either the module is not coming into effect, or the first section of the code isn’t working:

function required_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'node_form'){
    $form['#validate'][] = 'required_mysubmit';
  }
} 

Many thanks for your help on this

nevets’s picture

In required_form_alter() you could add a call to drupal_set_message() inside the 'if' statement to make sure you are getting there.

sylvie_n’s picture

I tried:

 function required_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'node_form'){
drupal_set_message('Being Called');
  }
} 

and then:

 function required_alter() {
  if($form_id == 'node_form'){
drupal_set_message('Being Called');
  }
} 

in a module on their own, and they didn't work.
This means that the module itself isn't working, right?
well the module shows up under admin, i have ticked it and pressed save... any ideas why the module isn't coming into play?
many thanks

nevets’s picture

In required_form_alter() before the 'if' statement add something like drupal_set_message("required_form_alter(..., ..., $form_id) to verify you are using the correct form id. You also need to change required_form_alter to expect the correct arguments, the correct declaration is required_form_alter($form_id, &$form)

sylvie_n’s picture

Hey,
It comes back with: required_form_alter(..., ..., Array
when i use drupal_set_message("required_form_alter(..., ..., $form_id");

nevets’s picture

Did you change the arguments per the second part of the comment?

sylvie_n’s picture

Yep:

function required_form_alter($form_id, &$form) {
drupal_set_message("required_form_alter(..., ..., $form_id");}

nevets’s picture

My bad, you had it right, should be of the form: hook_form_alter(&$form, $form_state, $form_id)

sylvie_n’s picture

Ok - so we're definately getting somewhere now. The second function is being called into play after submit.

When I use:

 function required_mysubmit($form, &$form_state) {
drupal_set_message('Form fields: ' . implode(', ',  array_keys($form_state['values'])));
  if (($form_state['values']['submitted_tree']['field_phone_number']== '')){
      form_set_error('required', t('Please enter in a phone number OR email address.'));

  }
} 

with the inclusion of ['submitted_tree'] in the third line, the message pops up after the submit button has been pressed.
However, it does this whether the phone number input field has been filled or not.
If I don't use ['submitted_tree'] and just use ['values'] then the message never comes up.

nevets’s picture

'submitted_tree' does not apply to CCK fields (its an element of webforms).

What does the message say, what are the form fields it prints out?

sylvie_n’s picture

ok. Thanks.
It prints out field_email_address, field_phone_number
The message says "Description field is required."
But does not print out the message with regards to the email address field, whether it is filled out or not.
The code i am using:

 function required_mysubmit($form, &$form_state) {
drupal_set_message('Form fields: ' . implode(', ',  array_keys($form_state['values'])));
  if (($form_state['values']['field_email_address']== '')){
      form_set_error('required', t('Please enter in an email address.'));

  }
} 
sylvie_n’s picture

ok, heres the thing.
when i add:

else {
form_set_error('else error=', $form_state['values']['field_email_address']);
}

it prints out 'Array' as the error message...

however, when i add:

else {
form_set_error('else error=', $form_state['values']['body']);
}

it prints out the body of the node correctly.
Since, the array code is as follows:

[title] => lihli
[taxonomy] => Array
(
[2] => 5
)

[menu] => Array
(
[link_title] =>
[parent] => primary-links:0
[weight] => 0
)

[teaser_include] => 1
[body] => sdsd sd sd sd sd sd sd sd sd s ds d sd s d sd
[format] => 3
[changed] => 1252959082
[op] => Save
[form_build_id] => form-fa745ae804e17faa081ae4412b268b5e
[form_token] => 2777b3b516c5d704b053af340cc3dbd2
[form_id] => job_classified_node_form
[name] => bebecohete
[date] => 2009-09-08 15:34:53 -0400
[log] =>
[status] => 1
[path] =>
[comment] => 1
[field_email_address] => Array
(
[0] => Array
(
[value] => iuh
)

)

How do i print out the value within the email address array? I have tried

else {
form_set_error('else error=', $form_state['values']['field_email_address']['value']);
}

but that doesn't work...

sylvie_n’s picture

Hi.
I have resolved this issue.
The code I am using, which has been tested and works is:

function required_form_alter(&$form, $form_state, $form_id){
if($form_id == 'job_classified_node_form'){
$form['#validate'][] = 'required_mysubmit';
  }
}

function required_mysubmit(&$form, $form_state) {

  if (($form['#post']['field_alternative_wage']['value']== '')&&($form['#post']['field_wage'][0]['value']== '')){
      form_set_error('required', t('Please enter in a wage or an alternative wage'));}

}