23 #include <QRegExpValidator> 34 : QValidator( parent )
36 , mDefaultValue( defaultValue )
37 , mDateFormat( dateFormat )
39 switch ( mField.
type() )
45 QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
46 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
50 mValidator =
new QIntValidator( parent );
55 case QVariant::Double:
61 if ( QLocale().decimalPoint() !=
'.' )
63 re = QStringLiteral(
"-?\\d{0,%1}([\\.%2]\\d{0,%3})?" ).arg( mField.
length() - mField.
precision() ).arg( QLocale().decimalPoint() ).arg( mField.
precision() );
67 re = QStringLiteral(
"-?\\d{0,%1}([\\.,]\\d{0,%2})?" ).arg( mField.
length() - mField.
precision() ).arg( mField.
precision() );
69 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
73 QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
74 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
80 if ( QLocale().decimalPoint() !=
'.' )
82 re = QStringLiteral(
"-?\\d*([\\.%1]\\d{0,%2})?" ).arg( QLocale().decimalPoint(), mField.
precision() );
86 re = QStringLiteral(
"-?\\d*([\\.]\\d{0,%1})?" ).arg( mField.
precision() );
88 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
92 mValidator =
new QDoubleValidator( parent );
97 case QVariant::LongLong :
102 mValidator =
nullptr;
117 ( mField.
type() == QVariant::Double
118 || mField.
type() == QVariant::Int
119 || mField.
type() == QVariant::LongLong
120 || mField.
type() == QVariant::Date
127 if ( s == mDefaultValue )
133 QValidator::State result = mValidator->validate( s, i );
136 else if ( mField.
type() == QVariant::String )
138 if ( s == mNullValue )
142 if ( mField.
length() > 0 && s.size() > mField.
length() )
144 if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
147 if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
153 else if ( mField.
type() == QVariant::Date )
155 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
159 QgsDebugMsg( QStringLiteral(
"unsupported type %1 for validation" ).arg( mField.
type() ) );
170 mValidator->fixup( s );
172 else if ( mField.
type() == QVariant::String && mField.
length() > 0 && s.size() > mField.
length() && s != mDefaultValue )
177 else if ( mField.
type() == QVariant::Date )
State validate(QString &s, int &i) const override
QgsFieldValidator(QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat="yyyy-MM-dd")
~QgsFieldValidator() override
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Encapsulate a field in an attribute table or data source.
void fixup(QString &s) const override