Skip to content

Hidden::isValid($intCount) silently ignores the $intCount parameter #203

Description

@rvanbaalen

Summary

Hidden::isValid($intCount) declares an $intCount parameter matching the parent Element::isValid($intCount) signature, but immediately overwrites it with the for loop initialiser on the next line. The parameter is never read.

Root cause

classes/ValidFormBuilder/Hidden.php:120-134:

public function isValid($intCount = null)
{
    $blnReturn = false;
    $intDynamicCount = ($this->isDynamicCounter()) ? $this->__validator->getValue() : 0;

    for ($intCount = 0; $intCount <= $intDynamicCount; $intCount++) {
        // ↑ overwrites the parameter — $intCount is now always 0 on first iteration
        $blnReturn = $this->__validator->validate($intCount);
        if (!$blnReturn) {
            break;
        }
    }

    return $blnReturn;
}

Expected behaviour

Element::isValid($intCount) uses the parameter meaningfully:

public function isValid($intCount = null)
{
    $blnReturn = false;
    $intDynamicCount = $this->getDynamicCount();

    if (is_null($intCount)) {
        // Loop through all dynamic positions
        for ($intCount = 0; $intCount <= $intDynamicCount; $intCount++) {
            $blnReturn = $this->__validator->validate($intCount);
            if (!$blnReturn) { break; }
        }
    } else {
        // Validate just one specific position
        $blnReturn = $this->__validator->validate($intCount);
    }

    return $blnReturn;
}

When $intCount is non-null, Element validates only that specific dynamic position. Hidden's override breaks this contract: $hidden->isValid(3) still validates positions 0 through N.

Impact

Low — the form pipeline calls isValid() without arguments, so the parameter-ignored path is not exercised in normal usage. But any caller that passes a specific position (e.g. during manual per-field validation of dynamic fields) gets silently wrong behaviour on Hidden fields compared to every other Element subclass.

Suggested fix

Apply the same null-check branching as the parent:

public function isValid($intCount = null)
{
    $blnReturn = false;
    $intDynamicCount = ($this->isDynamicCounter()) ? $this->__validator->getValue() : 0;

    if (is_null($intCount)) {
        for ($intCount = 0; $intCount <= $intDynamicCount; $intCount++) {
            $blnReturn = $this->__validator->validate($intCount);
            if (!$blnReturn) { break; }
        }
    } else {
        $blnReturn = $this->__validator->validate($intCount);
    }

    return $blnReturn;
}

Discovered while writing HiddenTest in #154.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions