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

        array_walk($args, 'trim');

        if ($args[0] == '') {
            throw new ValidatorConfigException("Validator 'unique' 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;
        }

        for ($i = count($args); $i < 4; $i++) {
            if (!isset($args[$i])) {
                $args[$i] = null;
            }
        }

        /** @var Model $modelClass */
        list($modelClass, $uniqueField, $exceptionValue, $exceptionField) = $args;

        if (is_string($exceptionValue)) {
            if (substr($exceptionValue, 0, 6) == 'field:') {
                $exceptionFieldName = substr($exceptionValue, 6);
                $exceptionFieldValue = $context[$exceptionFieldName] ?? null;

                if ($exceptionFieldValue === null) {
                    throw new ValidatorConfigException("Can not validate unique parameter={$parameter} when exception field name={$exceptionFieldName} is not present or has the value of null");
                }

                $exceptionValue = $exceptionFieldValue;
            } else {
                $exceptionValue = urldecode(trim($exceptionValue));
            }
        }

        if (!$modelClass::isUnique($uniqueField, $value, $exceptionValue, $exceptionField)) {
            return Message::getMessage(Message::NOT_UNIQUE, [
                'param' => $parameter,
                'value' => $value,
                'exceptionField' => $exceptionField,
                'exceptionValue' => $exceptionValue
            ]);
        }

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

        array_walk($args, 'trim');

        if ($args[0] == '') {
            throw new ValidatorConfigException("Validator 'unique' 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;
        }

        for ($i = count($args); $i < 4; $i++) {
            if (!isset($args[$i])) {
                $args[$i] = null;
            }
        }

        /** @var Model $modelClass */
        list($modelClass, $uniqueField, $exceptionValue, $exceptionField) = $args;

        if (is_string($exceptionValue)) {
            if (substr($exceptionValue, 0, 6) == 'field:') {
                $exceptionFieldName = substr($exceptionValue, 6);
                $exceptionFieldValue = $context[$exceptionFieldName] ?? null;

                if ($exceptionFieldValue === null) {
                    throw new ValidatorConfigException("Can not validate unique parameter={$parameter} when exception field name={$exceptionFieldName} is not present or has the value of null");
                }

                $exceptionValue = $exceptionFieldValue;
            } else {
                $exceptionValue = urldecode(trim($exceptionValue));
            }
        }

        if (!$modelClass::isUnique($uniqueField, $value, $exceptionValue, $exceptionField)) {
            return Message::getMessage(Message::NOT_UNIQUE, [
                'param' => $parameter,
                'value' => $value,
                'exceptionField' => $exceptionField,
                'exceptionValue' => $exceptionValue
            ]);
        }

        return null;
    }
protected function validateUnique ( $ value , string $ parameter , array $ args = [ ] , array $ rules = null , array $ context = null ) : ? string { if ( count ( $ args ) < 2 ) { throw new ValidatorConfigException ( "Validator 'unique' must have at least two defined arguments; parameter={$parameter}" ) ; } array_walk ( $ args , 'trim' ) ; if ( $ args [ 0 ] == '' ) { throw new ValidatorConfigException ( "Validator 'unique' 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 ; } for ( $ i = count ( $ args ) ; $ i < 4 ; $ i ++ ) { if ( ! isset ( $ args [ $ i ] ) ) { $ args [ $ i ] = null ; } } /** @var Model $modelClass */ list ( $ modelClass , $ uniqueField , $ exceptionValue , $ exceptionField ) = $ args ; if ( is_string ( $ exceptionValue ) ) { if ( substr ( $ exceptionValue , 0 , 6 ) == 'field:' ) { $ exceptionFieldName = substr ( $ exceptionValue , 6 ) ; $ exceptionFieldValue = $ context [ $ exceptionFieldName ] ?? null ; if ( $ exceptionFieldValue === null ) { throw new ValidatorConfigException ( "Can not validate unique parameter={$parameter} when exception field name={$exceptionFieldName} is not present or has the value of null" ) ; } $ exceptionValue = $ exceptionFieldValue ; } else { $ exceptionValue = urldecode ( trim ( $ exceptionValue ) ) ; } } if ( ! $ modelClass :: isUnique ( $ uniqueField , $ value , $ exceptionValue , $ exceptionField ) ) { return Message :: getMessage ( Message :: NOT_UNIQUE , [ 'param' => $ parameter , 'value' => $ value , 'exceptionField' => $ exceptionField , 'exceptionValue' => $ exceptionValue ] ) ; } return null ; }
Return error if value is not unique in database

@param mixed $value
@param string $parameter
@param array $args
@param array $rules other rules (Class\Name,uniqueField[,exceptionValue][,exceptionField])

@throws Exception
@return true|string
@example 'email' => 'email|unique:\Db\User,email,my@email.com'
@example
'id' => 'required|integer|min:1',
'email' => 'email|unique:\Db\User,email,field:id,id' // check if email exists in \Db\User model, but exclude ID with value from param 'id'
Return error if value is not unique in database