Skip to content

Validation

Mohamed Dawood edited this page Nov 9, 2021 · 1 revision

Validation

validating forms in flutter is very important but if you need to validate form field

so we made validations simpler effective and can be mixed together

how it work

Widget build(BuildContext context) {
  return TextFormField(
    validator: context.required(),
  );
}
  • why you need context
    • as you can see there no messages it will be fetched from the localized messages
    • currently supported locales is (ar,en,fr,ur)
    • so please add SimpleLocalizations.delegate to your material delegates if your app use localization otherwise the package will use arabic messages
  • how can i override the default messages
    • its simple wrap your material app with ValidationMessagesConfig
    return ValidationMessagesConfig(
      messages: (BuildContext context) {
        return ValidationMessages(
          required: "This field is required",
          mustBeRedditUrl: (v)=>"Value $v is not valid url"
        );
      },
      child: MaterialApp(),
    );
  • what if i need to override some screens messages or one field messages

    • just wrap it with ValidationMessagesConfig and set your messages
    • unset fields will be inherited from the parent one
  • how can i add multiple validations to one field

Widget build(BuildContext context) {
  // required is the only need the genric if it will be the first
  // or you can  do this `context.maxLength(4).required().minLength(1)`
  return TextFormField(
    validator: context.required<String>().maxLength(3).minLength(1),
  );
}
  • so what are the available validations
    • required. for any type
    • isDateAfter for DateTime fields
    • isDateBefore for DateTime fields
    • isDateBetween. for DateTime fields
    • dateAfter
    • dateBefore
    • dateBetween
    • isTimeOfDayAfter. for TimeOfDay
    • isTimeOfDayBefore. for TimeOfDay
    • isTimeOfDayBetween for TimeOfDay
    • timeOfDayAfter
    • timeOfDayBefore
    • timeOfDayBetween
    • maxLength
    • minLength
    • lengthInRange
    • dateTime
    • timeOfDay
    • dateAfter
    • dateBefore
    • dateBetween
    • timeOfDayAfter
    • timeOfDayBefore
    • timeOfDayBetween
    • number
    • integer
    • decimal
    • url
    • hexColor
    • localeEgyptianPhone
    • internationalEgyptianPhone
    • localeKsaPhone
    • internationalKsaPhone
    • email
    • githubUrl
    • redditUrl
    • instagramUrl
    • linkedinProfileUrl
    • twitterUserUrl
    • youtubeUrl
    • snapchatProfileUrl