src/Koldy/Validator.php
protected function validateExists($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        if (count($args) < 2) {
            throw new ValidatorConfigException("Validator 'exists' must have at least two defined arguments; parameter={$parameter}");
        }

        array_walk($args, 'trim');

        if ($args[0] == '') {
            throw new ValidatorConfigException("Validator 'exists' must have non-empty string for argument=0; parameter={$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        /** @var Model $modelClass */
        list($modelClass, $queryField) = $args;

        if ($modelClass::count([$queryField => $value]) == 0) {
            return Message::getMessage(Message::NO_RECORD, [
                'param' => $parameter,
                'value' => $value,
                'field' => $queryField
            ]);
        }

        return null;
    }
protected function validateExists($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        if (count($args) < 2) {
            throw new ValidatorConfigException("Validator 'exists' must have at least two defined arguments; parameter={$parameter}");
        }

        array_walk($args, 'trim');

        if ($args[0] == '') {
            throw new ValidatorConfigException("Validator 'exists' must have non-empty string for argument=0; parameter={$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        /** @var Model $modelClass */
        list($modelClass, $queryField) = $args;

        if ($modelClass::count([$queryField => $value]) == 0) {
            return Message::getMessage(Message::NO_RECORD, [
                'param' => $parameter,
                'value' => $value,
                'field' => $queryField
            ]);
        }

        return null;
    }
protected function validateExists ( $ value , string $ parameter , array $ args = [ ] , array $ rules = null , array $ context = null ) : ? string { if ( count ( $ args ) < 2 ) { throw new ValidatorConfigException ( "Validator 'exists' must have at least two defined arguments; parameter={$parameter}" ) ; } array_walk ( $ args , 'trim' ) ; if ( $ args [ 0 ] == '' ) { throw new ValidatorConfigException ( "Validator 'exists' must have non-empty string for argument=0; parameter={$parameter}" ) ; } //$value = $this->getValue($parameter); if ( $ value === null ) { return null ; } if ( ! is_scalar ( $ value ) ) { return Message :: getMessage ( Message :: PRIMITIVE , [ 'param' => $ parameter ] ) ; } $ value = trim ( ( string ) $ value ) ; if ( $ value === '' ) { return null ; } /** @var Model $modelClass */ list ( $ modelClass , $ queryField ) = $ args ; if ( $ modelClass :: count ( [ $ queryField => $ value ] ) == 0 ) { return Message :: getMessage ( Message :: NO_RECORD , [ 'param' => $ parameter , 'value' => $ value , 'field' => $ queryField ] ) ; } return null ; }
Return error if value does not exists in database

@param mixed $value
@param string $parameter
@param array $args
@param array $rules other rules (Class\Name,fieldName)

@throws Exception
@return true|string
@example 'user_id' => 'required|integer|exists:\Db\User,id' // e.g. user_id = 5, so this will check if there is record in \Db\User model under id=5
Return error if value does not exists in database