Validation across multiple fields

Jkello - September 27, 2008 - 11:07
Project:Validation API
Version:6.x-1.0
Component:Code
Category:support request
Priority:normal
Assigned:chinabruce
Status:closed
Description

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?

#1

Jkello - September 28, 2008 - 18:01

Any help please? Thanks.

#2

TapocoL - September 29, 2008 - 15:10
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):

<?php
$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.

#3

Anonymous (not verified) - October 13, 2008 - 15:22
Status:fixed» closed

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

#4

Jkello - October 26, 2008 - 14:40
Status:closed» 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?

#5

TapocoL - October 27, 2008 - 14:35
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').

#6

Anonymous (not verified) - November 10, 2008 - 14:41
Status:fixed» closed

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

#7

dropchew - March 2, 2009 - 06:29
Version:6.x-1.x-dev» 6.x-1.0
Status:closed» active

<?php
$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.

<?php
$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

<?php
$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 :)

#8

ginga8 - June 16, 2009 - 16:22

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

#9

chinabruce - September 28, 2009 - 02:01
Assigned to:Anonymous» chinabruce
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.

#10

System Message - October 12, 2009 - 02:10
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.