Skip to content

Commit

Permalink
Return a NULL variant in case of multi/not allow NULL
Browse files Browse the repository at this point in the history
Because an invalid variant would have not been set
in the feature form.
  • Loading branch information
elpaso authored and github-actions[bot] committed Dec 14, 2021
1 parent 1e94bd6 commit 93d434a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 44 deletions.
12 changes: 0 additions & 12 deletions python/core/auto_generated/vector/qgsvectorlayerutils.sip.in
Expand Up @@ -177,18 +177,6 @@ Returns ``True`` if the attribute value is valid for the field. Any constraint f
If the strength or origin parameter is set then only constraints with a matching strength/origin will be checked.
%End

static bool validateAttributeValue( const QVariant &value, const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors /Out/,
QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet,
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );
%Docstring
Tests a value to check whether it passes all constraints which are present on the corresponding field.
Returns ``True`` if the attribute value is valid for the field. Any constraint failures will be reported in the errors argument.
If the strength or origin parameter is set then only constraints with a matching strength/origin will be checked.

.. versionadded:: 3.24
%End


static QgsFeature createFeature( const QgsVectorLayer *layer,
const QgsGeometry &geometry = QgsGeometry(),
const QgsAttributeMap &attributes = QgsAttributeMap(),
Expand Down
7 changes: 1 addition & 6 deletions src/core/vector/qgsvectorlayerutils.cpp
Expand Up @@ -375,12 +375,6 @@ QVariant QgsVectorLayerUtils::createUniqueValueFromCache( const QgsVectorLayer *

bool QgsVectorLayerUtils::validateAttribute( const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors,
QgsFieldConstraints::ConstraintStrength strength, QgsFieldConstraints::ConstraintOrigin origin )
{
const QVariant value = feature.attribute( attributeIndex );
return QgsVectorLayerUtils::validateAttributeValue( value, layer, feature, attributeIndex, errors, strength, origin );
}

bool QgsVectorLayerUtils::validateAttributeValue( const QVariant &value, const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors, QgsFieldConstraints::ConstraintStrength strength, QgsFieldConstraints::ConstraintOrigin origin )
{
if ( !layer )
return false;
Expand All @@ -390,6 +384,7 @@ bool QgsVectorLayerUtils::validateAttributeValue( const QVariant &value, const Q

QgsFields fields = layer->fields();
QgsField field = fields.at( attributeIndex );
const QVariant value = feature.attribute( attributeIndex );
bool valid = true;
errors.clear();

Expand Down
11 changes: 0 additions & 11 deletions src/core/vector/qgsvectorlayerutils.h
Expand Up @@ -177,17 +177,6 @@ class CORE_EXPORT QgsVectorLayerUtils
QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet,
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );

/**
* Tests a value to check whether it passes all constraints which are present on the corresponding field.
* Returns TRUE if the attribute value is valid for the field. Any constraint failures will be reported in the errors argument.
* If the strength or origin parameter is set then only constraints with a matching strength/origin will be checked.
* \since QGIS 3.24
*/
static bool validateAttributeValue( const QVariant &value, const QgsVectorLayer *layer, const QgsFeature &feature, int attributeIndex, QStringList &errors SIP_OUT,
QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet,
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );


/**
* Creates a new feature ready for insertion into a layer. Default values and constraints
* (e.g., unique constraints) will automatically be handled. An optional attribute map can be
Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
Expand Up @@ -220,9 +220,9 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsVectorLayer *layer, int
toEmit = true;
}

hardConstraintsOk = QgsVectorLayerUtils::validateAttributeValue( value(), layer, ft, index, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );
hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );

softConstraintsOk = QgsVectorLayerUtils::validateAttributeValue( value(), layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
errors << softErrors;
}
else // invalid feature
Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -77,10 +77,10 @@ QVariant QgsValueRelationWidgetWrapper::value() const
}
}

// If there is no selection and allow NULL is not checked return invalid.
// If there is no selection and allow NULL is not checked return NULL.
if ( selection.isEmpty() && ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
return QVariant();
return QVariant( QVariant::Type::List );
}

QVariantList vl;
Expand Down
22 changes: 11 additions & 11 deletions tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -1337,11 +1337,11 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()

// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );
// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
// Because allowNull is false we have a NULL variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariant::Type::List ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );
//check if first feature checked correctly (NULL)
//check if first feature checked correctly (empty list)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
Expand All @@ -1355,12 +1355,12 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialite()

// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );
// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
// Because allowNull is false we have a NULL variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariant::Type::List ) );

cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );
//check if first feature checked correctly (blank)
//check if first feature checked correctly (empty list)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList( ) ) );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
Expand Down Expand Up @@ -1506,8 +1506,8 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
// FEATURE 4
w_favoriteauthors.setFeature( vl_json->getFeature( 4 ) );

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
// Because allowNull is false we have a NULL variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariant::Type::List ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

Expand All @@ -1527,12 +1527,12 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInSpatialiteTextFk()
// FEATURE 5
w_favoriteauthors.setFeature( vl_json->getFeature( 5 ) );

// Because allowNull is false we have an invalid variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( ) );
// Because allowNull is false we have a NULL variant here
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariant::Type::List ) );
cfg_favoriteauthors[ QStringLiteral( "AllowNull" ) ] = true;
w_favoriteauthors.setConfig( cfg_favoriteauthors );

//check if first feature checked correctly (blank)
//check if first feature checked correctly (empty list)
QCOMPARE( w_favoriteauthors.value(), QVariant( QVariantList() ) );

QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
Expand Down

0 comments on commit 93d434a

Please sign in to comment.