Problem/Motivation
Conditional Fields does not validate integers the same way that Drupal does. An integer field with a value of 0 and being Required by this module returns an error indicating the field is required despite the fact that the field is not empty
Steps to reproduce
1. Add two fields on any entity, for example taxonomy:
field_one - number (integer), minimum value set to 0;
field_two - boolean;
2. Go to admin/structure/conditional_fields/taxonomy_term/{your_vocabulary}
Add condition: field_one is required if field_two checked.
3. Edit term: check field_two, set 0 to field_one, save.
Proposed resolution
Update validation in ConditionalFieldsFormHelper.php -> dependentValidate:
use
if (empty($input_state) && $input_state !== '0')
instead of
if (empty($input_state))
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | wrong-validation-3512213-3.patch | 941 bytes | fromme |
Issue fork conditional_fields-3512213
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
fromme commentedOpen MR and add patch with fix.
Comment #4
benstallings commentedClaude Code says:
Classic PHP empty() gotcha. empty('0') returns true in PHP, so a conditionally-required integer field with a valid value of 0 gets flagged as empty and triggers a validation error.
The fix is correct and minimal — adds && $input_state !== '0' so that the string '0' (which is how form input arrives) is not treated as empty.
One thought: $input_state could also be 0 (integer) depending on the processing path, not just '0'. A slightly more robust check would be:
if (empty($input_state) && $input_state !== 0 && $input_state !== '0') {Or more idiomatically:
if ($input_state === '' || $input_state === NULL || $input_state === [])to be explicit about what "empty" means here. But given that line 579 casts null to [], and form input comes as strings, !== '0' likely covers the real case.Verdict: Good fix. Merge-worthy as-is.