src/gii/auth/Generator.php
/**
     * Generates validation rules for the specified table.
     * @param \yii\db\TableSchema $table the table schema
     * @return array the generated validation rules
     */
Generates validation rules for the specified table.
@param \yii\db\TableSchema $table the table schema
@return array the generated validation rules
Generates validation rules for the specified table . @param \ yii \ db \ TableSchema $table the table schema @return array the generated validation rules
public function generateRules($tableName)
    {
        $db = $this->getDbConnection();
        $table = $db->getTableSchema($tableName);
        $types = [];
        $lengths = [];
        foreach ($table->columns as $column) {
            if ($column->autoIncrement) {
                continue;
            }
            if (!$column->allowNull && $column->defaultValue === null) {
                $types['required'][] = $column->name;
            }
            switch ($column->type) {
                case Schema::TYPE_SMALLINT:
                case Schema::TYPE_INTEGER:
                case Schema::TYPE_BIGINT:
                case Schema::TYPE_TINYINT:
                    $types['integer'][] = $column->name;
                    break;
                case Schema::TYPE_BOOLEAN:
                    $types['boolean'][] = $column->name;
                    break;
                case Schema::TYPE_FLOAT:
                case Schema::TYPE_DOUBLE:
                case Schema::TYPE_DECIMAL:
                case Schema::TYPE_MONEY:
                    $types['number'][] = $column->name;
                    break;
                case Schema::TYPE_DATE:
                case Schema::TYPE_TIME:
                case Schema::TYPE_DATETIME:
                case Schema::TYPE_TIMESTAMP:
                case Schema::TYPE_JSON:
                    $types['safe'][] = $column->name;
                    break;
                default: 
                    if ($column->size > 0) {
                        $lengths[$column->size][] = $column->name;
                    } else {
                        $types['string'][] = $column->name;
                    }
            }
        }
        $rules = [];
        $driverName = $this->getDbDriverName();
        foreach ($types as $type => $columns) {
            if ($driverName === 'pgsql' && $type === 'integer') {
                $rules[] = "[['" . implode("', '", $columns) . "'], 'default', 'value' => null]";
            }
            $rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
        }
        foreach ($lengths as $length => $columns) {
            $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
        }
        $db = $this->getDbConnection();
        try {
            $uniqueIndexes = array_merge($db->getSchema()->findUniqueIndexes($table), [$table->primaryKey]);
            $uniqueIndexes = array_unique($uniqueIndexes, SORT_REGULAR);
            foreach ($uniqueIndexes as $uniqueColumns) {
                if (!$this->isColumnAutoIncremental($table, $uniqueColumns)) {
                    $attributesCount = count($uniqueColumns);
                    if ($attributesCount === 1) {
                        $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']";
                    } elseif ($attributesCount > 1) {
                        $columnsList = implode("', '", $uniqueColumns);
                        $rules[] = "[['$columnsList'], 'unique', 'targetAttribute' => ['$columnsList']]";
                    }
                }
            }
        } catch (NotSupportedException $e) {
        }
        foreach ($table->foreignKeys as $refs) {
            $refTable = $refs[0];
            $refTableSchema = $db->getTableSchema($refTable);
            if ($refTableSchema === null) {
                continue;
            }
            $refClassName = $this->generateClassName($refTable);
            unset($refs[0]);
            $attributes = implode("', '", array_keys($refs));
            $targetAttributes = [];
            foreach ($refs as $key => $value) {
                $targetAttributes[] = "'$key' => '$value'";
            }
            $targetAttributes = implode(', ', $targetAttributes);
            $rules[] = "[['$attributes'], 'exist', 'skipOnError' => true, 'targetClass' => $refClassName::className(), 'targetAttribute' => [$targetAttributes]]";
        }
        return $rules;
    }
public function generateRules ( $ tableName ) { $ db = $ this -> getDbConnection ( ) ; $ table = $ db -> getTableSchema ( $ tableName ) ; $ types = [ ] ; $ lengths = [ ] ; foreach ( $ table -> columns as $ column ) { if ( $ column -> autoIncrement ) { continue ; } if ( ! $ column -> allowNull && $ column -> defaultValue === null ) { $ types [ 'required' ] [ ] = $ column -> name ; } switch ( $ column -> type ) { case Schema :: TYPE_SMALLINT : case Schema :: TYPE_INTEGER : case Schema :: TYPE_BIGINT : case Schema :: TYPE_TINYINT : $ types [ 'integer' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_BOOLEAN : $ types [ 'boolean' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_FLOAT : case Schema :: TYPE_DOUBLE : case Schema :: TYPE_DECIMAL : case Schema :: TYPE_MONEY : $ types [ 'number' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_DATE : case Schema :: TYPE_TIME : case Schema :: TYPE_DATETIME : case Schema :: TYPE_TIMESTAMP : case Schema :: TYPE_JSON : $ types [ 'safe' ] [ ] = $ column -> name ; break ; default : if ( $ column -> size > 0 ) { $ lengths [ $ column -> size ] [ ] = $ column -> name ; } else { $ types [ 'string' ] [ ] = $ column -> name ; } } } $ rules = [ ] ; $ driverName = $ this -> getDbDriverName ( ) ; foreach ( $ types as $ type => $ columns ) { if ( $ driverName === 'pgsql' && $ type === 'integer' ) { $ rules [ ] = " [[' " . implode ( " ', ' " , $ columns ) . " '], 'default', 'value' => null] " ; } $ rules [ ] = " [[' " . implode ( " ', ' " , $ columns ) . " '], ' $ type '] " ; } foreach ( $ lengths as $ length => $ columns ) { $ rules [ ] = " [[' " . implode ( " ', ' " , $ columns ) . " '], 'string', 'max' =>  $ length ] " ; } $ db = $ this -> getDbConnection ( ) ; try { $ uniqueIndexes = array_merge ( $ db -> getSchema ( ) -> findUniqueIndexes ( $ table ) , [ $ table -> primaryKey ] ) ; $ uniqueIndexes = array_unique ( $ uniqueIndexes , SORT_REGULAR ) ; foreach ( $ uniqueIndexes as $ uniqueColumns ) { if ( ! $ this -> isColumnAutoIncremental ( $ table , $ uniqueColumns ) ) { $ attributesCount = count ( $ uniqueColumns ) ; if ( $ attributesCount === 1 ) { $ rules [ ] = " [[' " . $ uniqueColumns [ 0 ] . " '], 'unique'] " ; } elseif ( $ attributesCount > 1 ) { $ columnsList = implode ( " ', ' " , $ uniqueColumns ) ; $ rules [ ] = " [[' $ columnsList '], 'unique', 'targetAttribute' => [' $ columnsList ']] " ; } } } } catch ( NotSupportedException $ e ) { } foreach ( $ table -> foreignKeys as $ refs ) { $ refTable = $ refs [ 0 ] ; $ refTableSchema = $ db -> getTableSchema ( $ refTable ) ; if ( $ refTableSchema === null ) { continue ; } $ refClassName = $ this -> generateClassName ( $ refTable ) ; unset ( $ refs [ 0 ] ) ; $ attributes = implode ( " ', ' " , array_keys ( $ refs ) ) ; $ targetAttributes = [ ] ; foreach ( $ refs as $ key => $ value ) { $ targetAttributes [ ] = " ' $ key ' => ' $ value ' " ; } $ targetAttributes = implode ( ', ' , $ targetAttributes ) ; $ rules [ ] = " [[' $ attributes '], 'exist', 'skipOnError' => true, 'targetClass' =>  $ refClassName ::className(), 'targetAttribute' => [ $ targetAttributes ]] " ; } return $ rules ; }
Generates validation rules for the specified table.
Generates validation rules for the specified table .
// strings // Unique indexes rules // Avoid validating auto incremental columns // doesn't support unique indexes information...do nothing // Exist rules for foreign keys // Foreign key could point to non-existing table: https://github.com/yiisoft/yii2-gii/issues/34