Skip to content

Commit

Permalink
Fix constraint check when AllowMulti
Browse files Browse the repository at this point in the history
Fix #46366
  • Loading branch information
elpaso authored and github-actions[bot] committed Dec 11, 2021
1 parent d08d2fc commit f7d48d8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/core/vector/qgsvectorlayerutils.cpp
Expand Up @@ -431,9 +431,20 @@ bool QgsVectorLayerUtils::validateAttribute( const QgsVectorLayer *layer, const

if ( !exempt )
{
valid = valid && !value.isNull();

if ( value.isNull() )
const bool allowMulti { field.editorWidgetSetup().config().value( QStringLiteral( "AllowMulti" ), false ).toBool() };
bool multiViolated { false };
if ( allowMulti )
{
multiViolated = value == QStringLiteral( "{}" );
valid = valid && ( !value.isNull() && ! multiViolated );
}
else
{
valid = valid && !value.isNull();
}

if ( value.isNull() || multiViolated )
{
errors << QObject::tr( "value is NULL" );
notNullConstraintViolated = true;
Expand Down
21 changes: 15 additions & 6 deletions tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -32,6 +32,7 @@
QgsWkbTypes,
QgsCoordinateReferenceSystem,
QgsVectorLayerJoinInfo,
QgsEditorWidgetSetup,
NULL
)
from qgis.testing import start_app, unittest
Expand Down Expand Up @@ -266,7 +267,7 @@ def test_validate_attribute(self):
# field expression check
layer.setConstraintExpression(1, 'fldint>5')

f = QgsFeature(2)
f = QgsFeature(layer.fields(), 2)
f.setAttributes(["test123", 6])
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertTrue(res)
Expand All @@ -275,7 +276,6 @@ def test_validate_attribute(self):
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertFalse(res)
self.assertEqual(len(errors), 1)
print(errors)
# checking only for provider constraints
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1,
origin=QgsFieldConstraints.ConstraintOriginProvider)
Expand All @@ -287,7 +287,6 @@ def test_validate_attribute(self):
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertFalse(res)
self.assertEqual(len(errors), 1)
print(errors)

layer.setConstraintExpression(1, None)

Expand All @@ -301,7 +300,6 @@ def test_validate_attribute(self):
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertFalse(res)
self.assertEqual(len(errors), 1)
print(errors)

# checking only for provider constraints
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1,
Expand All @@ -319,7 +317,6 @@ def test_validate_attribute(self):
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertFalse(res)
self.assertEqual(len(errors), 1)
print(errors)

# checking only for provider constraints
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1,
Expand Down Expand Up @@ -354,7 +351,19 @@ def test_validate_attribute(self):
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 1)
self.assertFalse(res)
self.assertEqual(len(errors), 2)
print(errors)

# Test AllowMulti with no selected values, see GH #46366
setup = QgsEditorWidgetSetup('ValueRelation', {'AllowMulti': True})
layer.setEditorWidgetSetup(0, setup)
layer.setFieldConstraint(0, QgsFieldConstraints.ConstraintNotNull)
f.setAttribute(0, QVariant())
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 0)
self.assertFalse(res)
f.setAttribute(0, '{}')
self.assertEqual(len(errors), 1)
res, errors = QgsVectorLayerUtils.validateAttribute(layer, f, 0)
self.assertFalse(res)
self.assertEqual(len(errors), 1)

def testCreateUniqueValue(self):
""" test creating a unique value """
Expand Down

0 comments on commit f7d48d8

Please sign in to comment.