Skip to content
Permalink
Browse files
Fix constraint check when AllowMulti
Fix #46366
  • Loading branch information
elpaso authored and m-kuhn committed Dec 27, 2021
1 parent 39e0dac commit 7418f76a842f22b7f2349141a74990475ba3d2a0
Showing with 28 additions and 8 deletions.
  1. +13 −2 src/core/qgsvectorlayerutils.cpp
  2. +15 −6 tests/src/python/test_qgsvectorlayerutils.py
@@ -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;
@@ -32,6 +32,7 @@
QgsWkbTypes,
QgsCoordinateReferenceSystem,
QgsVectorLayerJoinInfo,
QgsEditorWidgetSetup,
NULL
)
from qgis.testing import start_app, unittest
@@ -257,7 +258,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)
@@ -266,7 +267,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)
@@ -278,7 +278,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)

@@ -292,7 +291,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,
@@ -310,7 +308,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,
@@ -345,7 +342,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 """

0 comments on commit 7418f76

Please sign in to comment.