validate 2 cck fields

thinguy - April 3, 2009 - 16:33
Project:Validation API
Version:6.x-1.0
Component:Documentation
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

I have 2 CCK select fields field_color(red,blue,yellow) and field_size(small,medium,large,x-large).

I would like to not allow certain combinations. Like Blue can't be medium. And Yellow can't be x-large.
How do I do this?

#1

presleyd - April 6, 2009 - 20:59
Status:active» duplicate

Check this issue #314163: Validation across multiple fields

#2

thinguy - April 8, 2009 - 03:56
Status:duplicate» active

That appears to be talking about adding integers to check total. I don't know how to turn that into "Blue" can't be "Medium"
Any help?

#3

presleyd - April 8, 2009 - 05:53

You are both talking about using the data from one field to validate the other. The tricky part is what is addressed in that post (how to pull one field's current state in order to validate the other. The rest is just some PHP and logic since you will need to use PHP validation. For example: To validate the field_size selection could probably use something like:

$value_field_color = (string) $form_state['values']['field_color'] ;

if ((($value_field_color == "blue") and ($value == "medium")) or (($value_field_color == "yellow") and ($value == "x-large")))
  return false;
else
  return true;

Since you are using select lists you could prol get away with the == operator and I thought it made the idea easier to read but I'd prol use strcasecmp() personally... hmmm I wonder if you'd have to cast the string first for that function, I'm sue you don't need to for == though.

At any rate, the real question here (about the module anyway) is still a duplicate, your question seems to be more about getting the PHP for a particular use case.

#4

thinguy - April 17, 2009 - 19:48

I can't seem to get this code to work. No matter where I put it or which field I set to validate it doesn't seem to kick-in.

#5

ecwize - June 30, 2009 - 06:37

Hi.
I have a problem with validating across multiple CCK fields when the current state of the field the validator is based on is empty.

<?php
// This (example) validator rule works nice :)
if ($value != "something") {
  return
true;
} elseif ((
$value == "something") and ($form_state['values']['field_myfield1'] == $form_state['values']['field_myfield2']) and ($form_state['values']['field_myfield2'] == $form_state['values']['field_myfield3'])){
  return
true;
} else {
  return
false;
}

// This (example) validator rule does *not* work :(
if ($value != "something") {
  return
true;
} elseif ((
$value == "something") and ($form_state['values']['field_myfield1'] == $form_state['values']['field_myfield2']) and ($form_state['values']['field_myfield3'] == '')){
  return
true;
} else {
  return
false;
}
?>

So, the question is: "how to match the CCK field's current *empty* state in the validator rule in order to validate the other field? In my case empty means that "- None -" (CCK default) value is displayed in the select box.

P.S.
Any explanation on the subject and where to look in (CCK or Validation API module's code I suppose) for the reasons of such behavior would be very appreciated.

Thanks.

#6

presleyd - July 2, 2009 - 15:46

Hmmm, I don't know this one either. I would presume what value is stored for the 'blank' option depends on which CCK field type it is (and maybe on which input widget is used). If you have access to the database itself you could look in there for some blank entries and see what's being stored. If you don't, tell me what CCK type you are using (and what widget) I'll try to duplicate it and poke around in my database to see what value it puts there when left blank.

#7

ecwize - July 4, 2009 - 05:02

Thanks for the answer, presleyd.

Well, the widget type is "Select list" and the field type is "Text". I have access to the database but I'm not so skilled with MySql unfortunately. Surely I'll follow your advice. But anyway, if you'll find time to find out the answer, please post it here.

...gone reading MySql mans...

#8

artscoop - September 16, 2009 - 19:25

Hi,

If you want to get input from a CCK Date Field, you can use the PHP code :

$value_field_date = $form_state['values']['field_mydate']['0'][value'];

You will get the string value entered in the field.

 
 

Drupal is a registered trademark of Dries Buytaert.