Closed (fixed)
Project:
Validation API
Version:
6.x-1.0
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Reporter:
Created:
27 Sep 2008 at 11:07 UTC
Updated:
12 Oct 2009 at 02:10 UTC
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
Comment #1
Jkello commentedAny help please? Thanks.
Comment #2
TapocoL commentedCurrently, 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):
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.
Comment #3
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #4
Jkello commentedDo you actually mean the following instead?:
Under where should I include this code snippet?
Comment #5
TapocoL commentedSorry, there is a mistake with the first line:
$value_a = (int) $value_aYou could change the line to this:
$value_a = (int) $valueor, 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').
Comment #6
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #7
dropchew commentedHi 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.
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
May I ask have this been tested by others besides mine? thanks. Btw this is a very useful module :)
Comment #8
ginga8 commentedI 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.
Any help would be appreciated.
Thanks
Comment #9
bdsupport commentedthe value of other field should be get like this:
$method = $form_state['values']['field_method'][0]['value'];
i tested it ok.