Flutter Validation is localized validation package wrapped around form_field_validator to deliver ready localized validation messages.
- Add to your dependencies:
dependencies:
flutter_validation:
git: git://github.com/MoaidAlrazhy/flutter_validation.git
- Add localizations delegates in your
MaterialApp
widget:
MaterialApp(
// ...
localizationsDelegates: [
ValidationLocalizations.delegate,
AttributeLocalizations.delegate,
],
supportedLocales: [
const Locale('en'),
const Locale('ar'),
const Locale.fromSubtags(languageCode: 'zh', countryCode: 'CN', scriptCode: 'Hans'),
/// add other locales for now (en, ar)
],
// ...
)
- Use it:
/// ...
TextFormField(
decoration: InputDecoration(
labelText: AttributeLocalizations.of(context).email,
),
// It's as easys as
validator: Validator.of(context).email,
)
/// ...
Validator.of(context).name // Validate name split legth >= 2
Validator.of(context).fullName // Validate name split legth >= 4
Validator.of(context).email // Validate email regex
Validator.of(context).required(
AttributeLocalizations.of(context).name,
) // Validate name to be required
Validator.of(context).empty(
AttributeLocalizations.of(context).name,
) // Validate name to be empty
Validator.of(context).phone
Validator.of(context).phoneNational(
IsoCode.US
);
Validator.of(context).match(
AttributeLocalizations.of(context).password,
AttributeLocalizations.of(context).passwordConfirmation,
() => 'confirm_password',
);
Validator.of(context).contains(
AttributeLocalizations.of(context).gender,
[
'male',
'femaile',
],
);
Validator.of(context).length(
AttributeLocalizations.of(context).password,
3,
);
Validator.of(context).minLength(
AttributeLocalizations.of(context).password,
6,
);
Validator.of(context).minLength(
AttributeLocalizations.of(context).password,
12,
);
Validator.of(context).lessThan(
AttributeLocalizations.of(context).age,
18,
);
Validator.of(context).lessThanOrEqualTo(
AttributeLocalizations.of(context).age,
18,
);
Validator.of(context).greaterThan(
AttributeLocalizations.of(context).age,
18,
);
Validator.of(context).greaterThanOrEqualTo(
AttributeLocalizations.of(context).age,
18,
);
Validator.of(context).isNum(AttributeLocalizations.of(context).price);
Validator.of(context).isInt(AttributeLocalizations.of(context).price);
Validator.of(context).lessThan(
AttributeLocalizations.of(context).age,
18,
);
// Example for starts with `https`
Validator.of(context).pattern(
'Url',
r'^(http|https)://',
);
As the package depend on form_field_validator so you can use the same logic using MultiValidator
:
MultiValidator([
Validator.of(context).required(
AttributeLocalizations.of(context).name
),
Validator.of(context).name,
// ...
])
As the library delivers a localized validation so it depends on both of validation_localizations and attribute_localizations.