79509134

Date: 2025-03-14 13:01:49
Score: 1
Natty:
Report link

Create a custom rule and check any array with the desired constraints. https://codeigniter4.github.io/userguide/libraries/validation.html#creating-a-rule-class

Sample class:

class IdRules
{
    /**
     * Use as 'required|valid_ids[100,intval,string]'
     */
    public function valid_ids($value = null, string $params, array $data = [], ?string $error = null): bool
    {
        $rules = explode(',', $params);

        if (! is_array($value)) {
            $error = 'Not array';

            return false;
        }

        if (count($value) > $rules[0]) {
            $error = 'The array is too large';

            return false;
        }

        foreach ($value as $k => $v) {
            // validation `ids` and return `bool`
            if (gettype($k) === $rules[1]) {}
            if (gettype($v) === $rules[2]) {}
        }

        return true;
    }
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: ozornick