23#include <QRegularExpression>
24#include <QRegularExpressionValidator>
35 : QValidator( parent )
37 , mDefaultValue( defaultValue )
38 , mDateFormat( dateFormat )
40 switch ( mField.
type() )
46 const QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
47 mValidator =
new QRegularExpressionValidator( QRegularExpression( re ), parent );
51 mValidator =
new QIntValidator( parent );
56 case QVariant::Double:
62 if ( QLocale().decimalPoint() !=
'.' )
64 re = QStringLiteral(
"-?\\d{0,%1}([\\.%2]\\d{0,%3})?" ).arg( mField.
length() - mField.
precision() ).arg( QLocale().decimalPoint() ).arg( mField.
precision() );
68 re = QStringLiteral(
"-?\\d{0,%1}([\\.,]\\d{0,%2})?" ).arg( mField.
length() - mField.
precision() ).arg( mField.
precision() );
70 mValidator =
new QRegularExpressionValidator( QRegularExpression( re ), parent );
74 const QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
75 mValidator =
new QRegularExpressionValidator( QRegularExpression( re ), parent );
81 if ( QLocale().decimalPoint() !=
'.' )
83 re = QStringLiteral(
"-?\\d*([\\.%1]\\d{0,%2})?" ).arg( QLocale().decimalPoint(), mField.
precision() );
87 re = QStringLiteral(
"-?\\d*([\\.]\\d{0,%1})?" ).arg( mField.
precision() );
89 mValidator =
new QRegularExpressionValidator( QRegularExpression( re ), parent );
93 mValidator =
new QDoubleValidator( parent );
98 case QVariant::LongLong :
103 mValidator =
nullptr;
118 ( mField.
type() == QVariant::Double
119 || mField.
type() == QVariant::Int
120 || mField.
type() == QVariant::LongLong
121 || mField.
type() == QVariant::Date
128 if ( s == mDefaultValue )
134 const QValidator::State result = mValidator->validate( s, i );
137 else if ( mField.
type() == QVariant::String )
139 if ( s == mNullValue )
143 if ( mField.
length() > 0 && s.size() > mField.
length() )
145 if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
148 if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
154 else if ( mField.
type() == QVariant::Date )
156 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
158 else if ( mField.
type() == QVariant::Map )
165 QStringLiteral(
"unsupported type %1 (%2) for validation" )
166 .arg( mField.
type() )
179 mValidator->fixup( s );
181 else if ( mField.
type() == QVariant::String && mField.
length() > 0 && s.size() > mField.
length() && s != mDefaultValue )
186 else if ( mField.
type() == QVariant::Date )
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
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
void fixup(QString &s) const override
Encapsulate a field in an attribute table or data source.
QString typeName() const
Gets the field type.