0 follower

Final Class Yiisoft\Yii\Gii\Validator\TableExistsHandler

InheritanceYiisoft\Yii\Gii\Validator\TableExistsHandler
ImplementsYiisoft\Validator\RuleHandlerInterface

An inline validator that checks if the attribute value refers to an existing class name.

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $connection )
$connection \Yiisoft\Db\Connection\ConnectionInterface

                public function __construct(
    private readonly ConnectionInterface $connection
) {
}

            
validate() public method

public \Yiisoft\Validator\Result validate ( mixed $value, object $rule, \Yiisoft\Validator\ValidationContext $context )
$value mixed
$rule object
$context \Yiisoft\Validator\ValidationContext

                public function validate(mixed $value, object $rule, ValidationContext $context): Result
{
    if (!$rule instanceof TableExistsRule) {
        throw new UnexpectedRuleException(TableExistsRule::class, $rule);
    }
    $result = new Result();
    if (!is_string($value)) {
        $result->addError(sprintf('Value must be a string, %s given.".', gettype($value)));
        return $result;
    }
    try {
        $tableSchema = $this->connection->getTableSchema($value);
    } catch (\Yiisoft\Db\Exception\Exception $e) {
        $result->addError(sprintf('The error occurred during fetching table schema: "%s".', $e));
        return $result;
    }
    if ($tableSchema === null) {
        $result->addError(sprintf('Table "%s" does not exist.', $value));
        return $result;
    }
    return $result;
}