Hi,

I would like to ask if the validation can be a combination of many fields.

For example in a certain node,

I have a field: field_a
I have another field: field_b

Both of these fields allows users to fill in integers.

I would like to validate that field_a + field_b is less than 30. Is that possible?

Comments

Jkello’s picture

Any help please? Thanks.

TapocoL’s picture

Status: Active » Fixed

Currently, it takes a little knowledge of Drupal's form system to know how to work with multiple fields. If I get enough response from the 1.0 release, I will be put more work into 2.0, which I will be trying to implement a multiple field validator, and other features as well.

Anyway to answer your question. If the fields are on the same form, then yes, you can create this in a validator using PHP code. First, lets say you are going to apply this validator to field_a. So, you would use $value in your php code to get the value for field_a, then you would use $form_state['values']['field_b'] for the value in field_b. So, you could have code look like this (do not use the php tags):

$value_a = (int) $value_a;
$value_b = (int) $form_state['values']['field_b'];
return (($value_a + $value_b) < 30);

Then, if you need each field to be an integer value, as well. I would set-up a separate validator like the following regex, and apply it to both fields.

/^[0-9]+$/

Make sure, you test both of these validators before going live, as I have not tested them myself.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

Jkello’s picture

Status: Closed (fixed) » Active

Do you actually mean the following instead?:

<?php
$value_a = (int) $form_state['values']['field_a'];
$value_b = (int) $form_state['values']['field_b'];
return (($value_a + $value_b) < 30);
?>

Under where should I include this code snippet?

TapocoL’s picture

Status: Active » Fixed

Sorry, there is a mistake with the first line:
$value_a = (int) $value_a

You could change the line to this:
$value_a = (int) $value

or, clear the line and change the 3rd line to this:
return ((((int) $value) + $value_b) < 30);

This is the code for the rule of a validator. Then, you would add this validator to 'field_a' (not 'field_b').

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

dropchew’s picture

Version: 6.x-1.x-dev » 6.x-1.0
Status: Closed (fixed) » Active
$value_a = (int) $value;
$value_b = (int) $form_state['values']['field_b'];
return (($value_a + $value_b) < 30);

Hi I tried the above code with a variation of my own using the same logic but it doesn't work. I tried to output the value of the other cck field to the error msg but it returns nothing.

$arguments[0] = $form_state['values']['field_b'];

field_b is iin same form with a value. But I am able to return the title of the node to the error msg, for testing purpose

$arguments[0] = $form_state['values']['title'];

May I ask have this been tested by others besides mine? thanks. Btw this is a very useful module :)

ginga8’s picture

I am trying to do the same, here is my code but it is not working

I have two dropdowns on the same for "field_start_time" and "field_end_time", which have the following values

-1| None
0| 12:00AM
1| 1:00AM
2| 2:00AM
3| 3:00AM
4| 4:00AM
5| 5:00AM
6| 6:00AM
7| 7:00AM
8| 8:00AM
9| 9:00AM
10| 10:00AM
11| 11:00AM
12| 12:00PM
13| 1:00PM
14| 2:00PM
15| 3:00PM
16| 4:00PM
17| 5:00PM
18| 6:00PM
19| 7:00PM
20| 8:00PM
21| 9:00PM
22| 10:00PM
23| 11:00PM

I want to make sure that they both have either none choose for both or a time for both, it cannot be a combination. As well, I want to make sure the start time is less than the end time.

$value_a = (int) $value;
$value_b = (int) $form_state['values']['field_end_time'];
if (($value_b == -1)&&($value_a == -1)){
   return true;
}
elseif(($value_b != -1)&&($value_a != -1)&&($value_a < $value_b)){
   return true;
}
return false;

Any help would be appreciated.

Thanks

bdsupport’s picture

Assigned: Unassigned » bdsupport
Status: Active » Fixed

the value of other field should be get like this:

$method = $form_state['values']['field_method'][0]['value'];

i tested it ok.

Status: Fixed » Closed (fixed)

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