Skip to content

Commit

Permalink
Don't allow empty field names or table names in new layer dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 30, 2022
1 parent 1461a81 commit 772033d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/gui/qgsnewvectortabledialog.cpp
Expand Up @@ -372,34 +372,45 @@ void QgsNewVectorTableDialog::validate()
mValidationErrors.clear();

const bool isSpatial { mGeomTypeCbo->currentIndex() > 0 };
if ( mTableNames.contains( mTableName->text(), Qt::CaseSensitivity::CaseInsensitive ) )
if ( mTableName->text().trimmed().isEmpty() )
{
mValidationErrors.push_back( tr( "Table <b>%1</b> already exists!" ).arg( mTableName->text() ) );
mValidationErrors.push_back( tr( "Table name cannot be empty" ) );
}
else if ( mTableNames.contains( mTableName->text(), Qt::CaseSensitivity::CaseInsensitive ) )
{
mValidationErrors.push_back( tr( "Table <b>%1</b> already exists" ).arg( mTableName->text() ) );
}
// Check for field names and geom col name
if ( isSpatial && fields().names().contains( mGeomColumn->text(), Qt::CaseSensitivity::CaseInsensitive ) )
{
mValidationErrors.push_back( tr( "Geometry column name <b>%1</b> cannot be equal to an existing field name!" ).arg( mGeomColumn->text() ) );
mValidationErrors.push_back( tr( "Geometry column name <b>%1</b> cannot be equal to an existing field name" ).arg( mGeomColumn->text() ) );
}
// No geometry and no fields? No party!
if ( ! isSpatial && fields().count() == 0 )
{
mValidationErrors.push_back( tr( "The table has no geometry column and no fields!" ) );
mValidationErrors.push_back( tr( "The table has no geometry column and no fields" ) );
}
// Check if precision is <= length
const QgsFields cFields { fields() };
for ( const QgsField &f : cFields )
{
if ( f.isNumeric() && f.length() >= 0 && f.precision() >= 0 && f.precision() > f.length() )
{
mValidationErrors.push_back( tr( "Field <b>%1</b>: precision cannot be greater than length!" ).arg( f.name() ) );
mValidationErrors.push_back( tr( "Field <b>%1</b>: precision cannot be greater than length" ).arg( f.name() ) );
}

for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
if ( f.name().trimmed().isEmpty() )
{
mValidationErrors.push_back( tr( "Field name cannot be empty" ) );
}
else
{
if ( f.name().compare( illegalName, Qt::CaseInsensitive ) == 0 )
for ( const QString &illegalName : std::as_const( mIllegalFieldNames ) )
{
mValidationErrors.push_back( tr( "<b>%1</b> is an illegal field name for this format and cannot be used" ).arg( f.name() ) );
if ( f.name().compare( illegalName, Qt::CaseInsensitive ) == 0 )
{
mValidationErrors.push_back( tr( "<b>%1</b> is an illegal field name for this format and cannot be used" ).arg( f.name() ) );
}
}
}
}
Expand Down

0 comments on commit 772033d

Please sign in to comment.