Hi guys,
wondering if you could help me figure out why my following custom module code isn't working. I am trying to make it so that one of two fields is required on a node form. I know for sure that the second function is being called, but not working as i want:

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) {
drupal_set_message('Form fields: ' . implode(', ',  array_keys($form_state['values'])));
  if (($form_state['values']['field_email_address']== '')&&($form_state['values']['field_phone_number']== '')){
      form_set_error('required', t('Please enter in an email address.'));

  }
} 

The problem lies in the following:

 if (($form_state['values']['field_email_address']== '')&&($form_state['values']['field_phone_number']== '')){
      form_set_error('required', t('Please enter in an email address or phone number.')); 

Any suggestions?

Comments

playwithtoy’s picture

what happens or doesnt happen?

you can try putting an else statement that returns the values so you can see what the if is testing

sylvie_n’s picture

hi there.
thanks for your response.
There is no error, it just doesn't return anything.
When I include:

else {form_set_error('required', t('Please enter in an email address.')); }
It prints the message "Please enter in an email address"
So the function is being called correctly, it just isn't TRUE.

I tried including:
else {return $form_state['values']['field_email_address'];}

This also doesn't produce anything. Have I got the code right here?
Thanks

playwithtoy’s picture

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

but if its passing the if test

if (($form_state['values']['field_email_address']== '')&&($form_state['values']['field_phone_number']== '')){
      form_set_error('required', t('Please enter in an email address or phone number.'));

that means that
both $form_state['values']['field_email_address'] and $form_state['values']['field_phone_number']
are
1) empty or
2) you are looking trying to access the form_state array improperly
are you sure that the $form_state array is set up the way you are travelling down it?

sylvie_n’s picture

hey,
i was writing the array improperly. it is ['field_email_address']['value']
However, it doesn't produce anything using else {
form_set_error('else error=', $form_state['field_email_address']['value']);
even though I can see on the print out of the array that it does have a value (if you look at the below, you can see [value] => zdczx).

However, I'm not sure how far i should be writing down the array.
the array prints out as follows:
Array
(
[title] => Array
(
[#type] => textfield
[#title] => Classified Ad Title
[#required] => 1
[#default_value] =>
[#maxlength] => 255
[#weight] => -5
[#post] => Array
(
[title] =>
[taxonomy] => Array
(
[2] =>
)

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

[teaser_include] => 1
[body] =>
[format] => 1
[changed] =>
[op] => Save
[form_build_id] => form-c0e1cb91eae914a1be7900041d6fe34e
[form_token] => 2777b3b516c5d704b053af340cc3dbd2
[form_id] => job_classified_node_form
[log] =>
[name] => bebecohete
[date] =>
[status] => 1
[promote] => 1
[path] =>
[comment] => 2
[field_email_address] => Array
(
[0] => Array
(
[value] => zdczx
)

)

[field_phone_number] => Array
(
[0] => Array
(
[value] => 89789
)

)

}

can you see how much of this i should be following down?
thanks

sylvie_n’s picture

When i enter in:

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

it prints out 'Array' as the error message.

However, when i enter in

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

it prints out the correct body value as the error message.

I need to get to the ['value'] within the field_email_address array.
However, when i enter in

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

it doesn't show anything

playwithtoy’s picture

print $form_state['field_email_address'][0]['value'];

sylvie_n’s picture

hey!
thanks. But that doesn't print out anything...

playwithtoy’s picture

what array are you printing out up there?
is it $form_state

if it is
what happens when u do a print_r($form_state["field_email_address"]);

sylvie_n’s picture

Hi!
The array I am copying from is:
print_r($form);

print_r($form_state["field_email_address"]); doesn't print out anything.
Neither does print_r($form_state["field_email_address"]);

playwithtoy’s picture

print_r ($form['field_email_address'][0]['value']);

sylvie_n’s picture

Hey,
when I have the email address field filled in as: email@hotmail.com, the following array is printed:

Array ( [#type] => textfield [#default_value] => email@hotmail.com [#autocomplete_path] => [#size] => 60 [#attributes] => Array ( [class] => text ) [#title] => email address [#description] => [#required] => [#field_name] => field_email_address [#type_name] => job_classified [#delta] => 0 [#columns] => Array ( [0] => value ) [#maxlength] => [#post] => Array ( [title] => [taxonomy] => Array ( [2] => ) [menu] => Array ( [link_title] => [parent] => primary-links:0 [weight] => 0 ) [teaser_include] => 1 [body] => [format] => 1 [changed] => [op] => Save [form_build_id] => form-2af92c8ae438e108c99b7d8682225142 [form_token] => 2777b3b516c5d704b053af340cc3dbd2 [form_id] => job_classified_node_form [log] => [name] => bebecohete [date] => [status] => 1 [promote] => 1 [comment] => 2 [path] => [field_email_address] => Array ( [0] => Array ( [value] => email@hotmail.com ) ) ) [#programmed] => [#tree] => 1 [#parents] => Array ( [0] => field_email_address [1] => 0 [2] => value ) [#array_parents] => Array ( [0] => field_email_address [1] => 0 [2] => value ) [#weight] => 0 [#processed] => 1 [#input] => 1 [#process] => Array ( [0] => form_expand_ahah ) [#name] => field_email_address[0][value] [#id] => edit-field-email-address-0-value [#value] => email@hotmail.com [#needs_validation] => 1 [#defaults_loaded] => 1 [#sorted] => 1 )

playwithtoy’s picture

if (($form['field_email_address'][0]['value']== ''))

sylvie_n’s picture

Hi there,

Thanks. But when I tried:

  if (($form['field_email_address'][0]['value']== '')){
      form_set_error('required', t('Please enter in an email address.'));}

else {print_r ($form['field_email_address'][0]['value']); }
} 

with the email address field blank, the else statement was printed, as follows:

Array ( [#type] => textfield [#default_value] => [#autocomplete_path] => [#size] => 60 [#attributes] => Array ( [class] => text ) [#title] => email address [#description] => [#required] => [#field_name] => field_email_address [#type_name] => job_classified [#delta] => 0 [#columns] => Array ( [0] => value ) [#maxlength] => [#post] => Array ( [title] => [taxonomy] => Array ( [1] => ) [menu] => Array ( [link_title] => [parent] => primary-links:0 [weight] => 0 ) [teaser_include] => 1 [body] => [format] => 1 [changed] => [op] => Save [form_build_id] => form-9c6bee535a27362e498df1cbd0cbbabd [form_token] => a4555ef2ad10760416adb76142a6743f [form_id] => job_classified_node_form [log] => [name] => bebecohete [date] => [status] => 1 [path] => [comment] => 2 [field_area] => Array ( [value] => Santo Domingo ) [field_currency_type] => Array ( [value] => ) [field_price] => Array ( [0] => Array ( [value] => ) ) [field_alternative_price] => Array ( [value] => ) [field_email_address] => Array ( [0] => Array ( [value] => ) ) ) [#programmed] => [#tree] => 1 [#parents] => Array ( [0] => field_email_address [1] => 0 [2] => value ) [#array_parents] => Array ( [0] => field_email_address [1] => 0 [2] => value ) [#weight] => 0 [#processed] => 1 [#input] => 1 [#process] => Array ( [0] => form_expand_ahah ) [#name] => field_email_address[0][value] [#id] => edit-field-email-address-0-value [#value] => [#needs_validation] => 1 [#defaults_loaded] => 1 [#sorted] => 1 )

If the above array that is printed out is equal to ($form['field_email_address'][0]['value']), then this is not the field value which we are looking for and not equal to zero...

playwithtoy’s picture

in the else statement put what you want
in the orig question
you jsut wanted the proper test for the if statemnt
whcih is correct now

sylvie_n’s picture

the thing is, is the else statement is being performed whether the email address field is filled in or not!

if (($form['field_email_address'][0]['value']== ''))

so if the email address field is blank, i.e. value = zero, then the else statement should not be carried out.

I want something to happen when the email address is not filled out...this isn't happening, so the if statement isn't working. The if statement is saying, "if the email field is not filled in" perform "such and such". Well, "such and such" isn't happening when the email field is blank.

What I would like to know is the if statement for "field_email_address = nothing"

thanks

netbear’s picture

if ($form['field_email_address'][0]['value']== '') {
  form_error($form['field_email_address'], t('The email address is requiered'));
}
sylvie_n’s picture

yep - thats what I tried...no luck.
The error message doesn't print out when the email address is blank

netbear’s picture

This snippet is copied from my site where it works as requered.

sylvie_n’s picture

I copied your code exactly, left the email address field blank, and it doesn't print the error message.
It instead performs the else statement.

What version of drupal are you using? I'm using 6.
thanks

netbear’s picture

Well, I too copied this from working site where it works correctly... drupal 6.14.
try to modify this condition this way...

if(!isset($form['field_email_address'][0]['value']) || !$form['field_email_address'][0]['value']) {
sylvie_n’s picture

Hey,
This IF statement also doesn't work. It prints out the else statement.
Thanks

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'));}

}