public function requireOneOf(array $fieldnames, $count = 1) {
        $result = $this->addValidator('*', function ($data, Validation $validation) use ($fieldnames, $count) {
            $hasCount = 0;
            $flattened = [];

            foreach ($fieldnames as $name) {
                $flattened = array_merge($flattened, (array)$name);

                if (is_array($name)) {
                    // This is an array of required names. They all must match.
                    $hasCountInner = 0;
                    foreach ($name as $nameInner) {
                        if (isset($data[$nameInner]) && $data[$nameInner]) {
                            $hasCountInner++;
                        } else {
                            break;
                        }
                    }
                    if ($hasCountInner >= count($name)) {
                        $hasCount++;
                    }
                } elseif (isset($data[$name]) && $data[$name]) {
                    $hasCount++;
                }

                if ($hasCount >= $count) {
                    return true;
                }
            }

            $messageFields = array_map(function ($v) {
                if (is_array($v)) {
                    return '('.implode(', ', $v).')';
                }
                return $v;
            }, $fieldnames);

            if ($count === 1) {
                $message = sprintft('One of %s are required.', implode(', ', $messageFields));
            } else {
                $message = sprintft('%1$s of %2$s are required.', $count, implode(', ', $messageFields));
            }

            $validation->addError('missing_field', $flattened, [
                'message' => $message
            ]);
            return false;
        });

        return $result;
    }
public function requireOneOf(array $fieldnames, $count = 1) {
        $result = $this->addValidator('*', function ($data, Validation $validation) use ($fieldnames, $count) {
            $hasCount = 0;
            $flattened = [];

            foreach ($fieldnames as $name) {
                $flattened = array_merge($flattened, (array)$name);

                if (is_array($name)) {
                    // This is an array of required names. They all must match.
                    $hasCountInner = 0;
                    foreach ($name as $nameInner) {
                        if (isset($data[$nameInner]) && $data[$nameInner]) {
                            $hasCountInner++;
                        } else {
                            break;
                        }
                    }
                    if ($hasCountInner >= count($name)) {
                        $hasCount++;
                    }
                } elseif (isset($data[$name]) && $data[$name]) {
                    $hasCount++;
                }

                if ($hasCount >= $count) {
                    return true;
                }
            }

            $messageFields = array_map(function ($v) {
                if (is_array($v)) {
                    return '('.implode(', ', $v).')';
                }
                return $v;
            }, $fieldnames);

            if ($count === 1) {
                $message = sprintft('One of %s are required.', implode(', ', $messageFields));
            } else {
                $message = sprintft('%1$s of %2$s are required.', $count, implode(', ', $messageFields));
            }

            $validation->addError('missing_field', $flattened, [
                'message' => $message
            ]);
            return false;
        });

        return $result;
    }
public function requireOneOf ( array $ fieldnames , $ count = 1 ) { $ result = $ this -> addValidator ( '*' , function ( $ data , Validation $ validation ) use ( $ fieldnames , $ count ) { $ hasCount = 0 ; $ flattened = [ ] ; foreach ( $ fieldnames as $ name ) { $ flattened = array_merge ( $ flattened , ( array ) $ name ) ; if ( is_array ( $ name ) ) { // This is an array of required names. They all must match. $ hasCountInner = 0 ; foreach ( $ name as $ nameInner ) { if ( isset ( $ data [ $ nameInner ] ) && $ data [ $ nameInner ] ) { $ hasCountInner ++ ; } else { break ; } } if ( $ hasCountInner >= count ( $ name ) ) { $ hasCount ++ ; } } elseif ( isset ( $ data [ $ name ] ) && $ data [ $ name ] ) { $ hasCount ++ ; } if ( $ hasCount >= $ count ) { return true ; } } $ messageFields = array_map ( function ( $ v ) { if ( is_array ( $ v ) ) { return '(' . implode ( ', ' , $ v ) . ')' ; } return $ v ; } , $ fieldnames ) ; if ( $ count === 1 ) { $ message = sprintft ( 'One of %s are required.' , implode ( ', ' , $ messageFields ) ) ; } else { $ message = sprintft ( '%1$s of %2$s are required.' , $ count , implode ( ', ' , $ messageFields ) ) ; } $ validation -> addError ( 'missing_field' , $ flattened , [ 'message' => $ message ] ) ; return false ; } ) ; return $ result ; }
Require one of a given set of fields in the schema.

@param array $fieldnames The field names to require.
@param int $count The count of required items.
@return Schema Returns `$this` for fluent calls.
Require one of a given set of fields in the schema .