Here is an example of a custom validator from another thread:
In the snippet code you can access the value of the field (the validator is attached to) with $value, to get the values of other fields use $validator->fields["your_field_name"].
<?php
$success = true;
if (empty(trim($value))){ // field1 is empty
if ($validator->fields['color'] != 'blue') { // color is not blue
$success = false;
}
}
if (!$success) {
$validator->addError($key,'fill in field1');
}
return $success;
The error message should appear if field1 is empty AND color is green OR red. If color is blue field1 can be empty.
With my snippet field1 shows every time ‘fill in field1’.
The error log shows a message: PHP warning: Undefined array key “color”. What can I do now?