Hi,
great work so far. Thank you for contributing!

I try to set up a PHP rule like this:

if ( $form_state['values']['field_AnotherFieldOnSameForm'] == 'SomeVal') {
   return ($value != 'ValThisField');
}

It should give an error if the field AnotherFieldOnSameForm contains SomeVal AND the field i want to validate contains ValThisField

Is this possible and how? My example gives an error every time.
Merci

Comments

TapocoL’s picture

Status: Active » Fixed

First of all, if you included the <?php tags in your rule, take them out.

Secondly, you should always return something because the return value is tested to be true (or a value that can be represented as true in PHP). If it is not, then it will throw an error. So, right now if field_AnotherFieldOnSameForm != 'SomeVal', it will fail. I am just assuming that you want it to pass if that field does not equal SomeVal. If you do want it to fail, then there is not technically a problem with the rule you setup.

Here is an example validator rule that will only check the related field to equal 'jkl;' if a field called 'subject' is equal to 'asdf'. If 'subject' is not equal to 'asdf', there are no problems.
Name: multi_field
Type: PHP
Rule:

if ( $form_state['values']['subject'] == 'asdf') {
   return ($value != 'jkl;');
}
return TRUE;

Message: If subject is 'asdf', then comment cannot be 'jkl;'.

I set it up on a comment form. So, the field values should be the following:
Form ID: comment_form
Field Name: comment
Validator: multi_field
Allow Empty?: (checking doesn't matter because the comment field is always required)
Message: Message: If subject is 'asdf', then comment cannot be 'jkl;'.

Hope this helps.

Status: Fixed » Closed (fixed)

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