Skip to content
Permalink
Browse files

Constraints fail on non existent joined fields

  • Loading branch information
pblottiere committed Jul 11, 2017
1 parent 94034c5 commit 588fe49aee5f7d3aa637f2d15a6d9196e7f503b4
Showing with 63 additions and 38 deletions.
  1. +50 −32 src/gui/editorwidgets/core/qgseditorwidgetwrapper.cpp
  2. +13 −6 src/gui/qgsattributeform.cpp
@@ -126,53 +126,71 @@ void QgsEditorWidgetWrapper::updateConstraint( const QgsFeature &ft, QgsFieldCon

void QgsEditorWidgetWrapper::updateConstraint( const QgsVectorLayer *layer, int index, const QgsFeature &ft, QgsFieldConstraints::ConstraintOrigin constraintOrigin )
{
QStringList errors;
QStringList softErrors;
QStringList expressions;
QStringList descriptions;
bool toEmit( false );
QgsField field = layer->fields().at( index );
bool hardConstraintsOk( true );
bool softConstraintsOk( true );

QgsField field = layer->fields().at( index );
QString expression = field.constraints().constraintExpression();
QStringList expressions, descriptions;

if ( ! expression.isEmpty() )
{
expressions << expression;
descriptions << field.constraints().constraintDescription();
toEmit = true;
}

if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintNotNull )
if ( ft.isValid() )
{
descriptions << tr( "Not NULL" );
if ( !expression.isEmpty() )
if ( ! expression.isEmpty() )
{
expressions << field.name() + QStringLiteral( " IS NOT NULL" );
expressions << expression;
descriptions << field.constraints().constraintDescription();
toEmit = true;
}
else
{
expressions << QStringLiteral( "IS NOT NULL" );
}
toEmit = true;
}

if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
{
descriptions << tr( "Unique" );
if ( !expression.isEmpty() )
if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintNotNull )
{
expressions << field.name() + QStringLiteral( " IS UNIQUE" );
descriptions << tr( "Not NULL" );
if ( !expression.isEmpty() )
{
expressions << field.name() + QStringLiteral( " IS NOT NULL" );
}
else
{
expressions << QStringLiteral( "IS NOT NULL" );
}
toEmit = true;
}
else

if ( field.constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
{
expressions << QStringLiteral( "IS UNIQUE" );
descriptions << tr( "Unique" );
if ( !expression.isEmpty() )
{
expressions << field.name() + QStringLiteral( " IS UNIQUE" );
}
else
{
expressions << QStringLiteral( "IS UNIQUE" );
}
toEmit = true;
}
toEmit = true;

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

softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
errors << softErrors;
}
else // invalid feature
{
if ( ! expression.isEmpty() )
{
hardConstraintsOk = true;
softConstraintsOk = false;

QStringList errors;
bool hardConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, errors, QgsFieldConstraints::ConstraintStrengthHard, constraintOrigin );
errors << "Invalid feature";

QStringList softErrors;
bool softConstraintsOk = QgsVectorLayerUtils::validateAttribute( layer, ft, index, softErrors, QgsFieldConstraints::ConstraintStrengthSoft, constraintOrigin );
errors << softErrors;
toEmit = true;
}
}

mValidConstraint = hardConstraintsOk && softConstraintsOk;
mIsBlockingCommit = !hardConstraintsOk;
@@ -747,15 +747,23 @@ void QgsAttributeForm::updateConstraint( const QgsFeature &ft, QgsEditorWidgetWr
{
QgsFieldConstraints::ConstraintOrigin constraintOrigin = mLayer->isEditable() ? QgsFieldConstraints::ConstraintOriginNotSet : QgsFieldConstraints::ConstraintOriginLayer;

if ( eww->layer()->fields().fieldOrigin( QgsFields::OriginJoin ) )
if ( eww->layer()->fields().fieldOrigin( eww->fieldIdx() ) == QgsFields::OriginJoin )
{
int srcFieldIdx;
const QgsVectorLayerJoinInfo *info = eww->layer()->joinBuffer()->joinForFieldIndex( eww->fieldIdx(), eww->layer()->fields(), srcFieldIdx );

if ( info && info->joinLayer() && mJoinedFeatures.contains( info ) && info->isDynamicFormEnabled() )
if ( info && info->joinLayer() && info->isDynamicFormEnabled() )
{
eww->updateConstraint( info->joinLayer(), srcFieldIdx, mJoinedFeatures[info], constraintOrigin );
return;
if ( mJoinedFeatures.contains( info ) )
{
eww->updateConstraint( info->joinLayer(), srcFieldIdx, mJoinedFeatures[info], constraintOrigin );
return;
}
else // if we are here, it means there's not joined field for this feature
{
eww->updateConstraint( QgsFeature() );
return;
}
}
}

@@ -1972,8 +1980,7 @@ void QgsAttributeForm::updateJoinedFields( const QgsEditorWidgetWrapper &eww )

QgsFeature joinFeature = mLayer->joinBuffer()->joinedFeatureOf( info, formFeature );

if ( joinFeature.isValid() )
mJoinedFeatures[info] = joinFeature;
mJoinedFeatures[info] = joinFeature;

QStringList *subsetFields = info->joinFieldNamesSubset();
if ( subsetFields )

0 comments on commit 588fe49

Please sign in to comment.
You can’t perform that action at this time.