Skip to content

Commit

Permalink
Merge pull request #46452 from elpaso/bugfix-gh46364-form-constraints…
Browse files Browse the repository at this point in the history
…-issues
  • Loading branch information
m-kuhn authored Dec 14, 2021
2 parents cb06b6f + 8e389b0 commit 618d575
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ The optional seed value can be used as a basis for generated values.
QgsFieldConstraints::ConstraintStrength strength = QgsFieldConstraints::ConstraintStrengthNotSet,
QgsFieldConstraints::ConstraintOrigin origin = QgsFieldConstraints::ConstraintOriginNotSet );
%Docstring
Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.
Tests a feature attribute 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.
%End
Expand Down
2 changes: 1 addition & 1 deletion src/core/vector/qgsvectorlayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ bool QgsVectorLayerUtils::validateAttribute( const QgsVectorLayer *layer, const

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

Expand Down
2 changes: 1 addition & 1 deletion src/core/vector/qgsvectorlayerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class CORE_EXPORT QgsVectorLayerUtils
static QVariant createUniqueValueFromCache( const QgsVectorLayer *layer, int fieldIndex, const QSet<QVariant> &existingValues, const QVariant &seed = QVariant() );

/**
* Tests an attribute value to check whether it passes all constraints which are present on the corresponding field.
* Tests a feature attribute 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.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Original file line number Diff line number Diff line change
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
27 changes: 24 additions & 3 deletions src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,9 +1234,9 @@ void QgsAttributeForm::registerContainerInformation( QgsAttributeForm::Container
}
}

bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions )
bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const
{
bool valid( true );
bool valid{ true };

for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
Expand All @@ -1258,6 +1258,27 @@ bool QgsAttributeForm::currentFormValidConstraints( QStringList &invalidFields,
return valid;
}

bool QgsAttributeForm::currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const
{
bool valid{ true };

for ( QgsWidgetWrapper *ww : std::as_const( mWidgets ) )
{
QgsEditorWidgetWrapper *eww = qobject_cast<QgsEditorWidgetWrapper *>( ww );
if ( eww )
{
if ( eww->isBlockingCommit() )
{
invalidFields.append( eww->field().displayName() );
descriptions.append( eww->constraintFailureReason() );
valid = false; // continue to get all invalid fields
}
}
}

return valid;
}

void QgsAttributeForm::onAttributeAdded( int idx )
{
mPreventFeatureRefresh = false;
Expand Down Expand Up @@ -1454,7 +1475,7 @@ void QgsAttributeForm::synchronizeState()
if ( mMode != QgsAttributeEditorContext::SearchMode )
{
QStringList invalidFields, descriptions;
mValidConstraints = currentFormValidConstraints( invalidFields, descriptions );
mValidConstraints = currentFormValidHardConstraints( invalidFields, descriptions );

if ( isEditable && mContext.formMode() == QgsAttributeEditorContext::Embed )
{
Expand Down
3 changes: 2 additions & 1 deletion src/gui/qgsattributeform.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void updateConstraint( const QgsFeature &ft, QgsEditorWidgetWrapper *eww );
void updateLabels();
bool currentFormValuesFeature( QgsFeature &feature );
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions );
bool currentFormValidConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
bool currentFormValidHardConstraints( QStringList &invalidFields, QStringList &descriptions ) const;
QList<QgsEditorWidgetWrapper *> constraintDependencies( QgsEditorWidgetWrapper *w );

Q_DECL_DEPRECATED QgsRelationWidgetWrapper *setupRelationWidgetWrapper( const QgsRelation &rel, const QgsAttributeEditorContext &context ) SIP_DEPRECATED;
Expand Down
22 changes: 11 additions & 11 deletions tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
Original file line number Diff line number Diff line change
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 618d575

Please sign in to comment.